Execution Time Limit in PHP: max_execution_time Explained

Have you ever run a PHP script that works perfectly with small data but suddenly appears to hang when processing a large file, importing records, or generating a report? Many developers first suspect a database issue or an infinite loop. Sometimes, PHP has simply decided that the script has taken long enough and stopped it.

PHP has a built-in execution time limit to prevent a single script from consuming server resources forever. This limit is controlled mainly by the max_execution_time setting in PHP configuration.

In this article, we will see how the PHP execution time limit works, how to change it using different methods, and when each approach is suitable.

What is the PHP execution time limit?

The PHP execution time limit defines the maximum amount of time a PHP script is allowed to run before PHP terminates it.

The setting that controls this behavior is:

max_execution_time

The value is measured in seconds. For example, if the value is set to 30, PHP allows the script to run for 30 seconds. If the script does not finish within that time, PHP stops execution and usually displays a fatal error similar to:

Fatal error: Maximum execution time of 30 seconds exceeded

The default value is commonly 30 seconds, but it depends on the PHP installation and hosting environment.

This limit exists for a good reason. Without it, a script with a forgotten loop could keep running and consume server resources indefinitely. A small typo in a while loop should not bring down an entire website.

How to check the current PHP execution time limit

Before changing the execution time limit, it is useful to know the current value configured on your server.

You can check it using the ini_get() function:

<?php
echo ini_get('max_execution_time');
?>

The output will show the configured limit in seconds.

You can also check it from a PHP configuration page created with phpinfo(). The max_execution_time value appears under the PHP Core section.

If you need more details about checking PHP configuration values, see this guide on using phpinfo() in PHP.

Changing PHP execution time limit using php.ini

The recommended way to change the PHP execution time limit is by modifying the php.ini configuration file.

Open your php.ini file and update the following setting:

max_execution_time = 60

The above configuration increases the maximum execution time to 60 seconds.

After changing the file, restart your web server for the new setting to take effect.

For example, when using Apache, you may need to restart Apache:

sudo service apache2 restart

The exact restart command depends on your operating system and server setup.

The php.ini method is generally preferred because it provides a centralized configuration and applies consistently across PHP scripts.

Changing PHP execution time limit using set_time_limit()

PHP provides the set_time_limit() function to change the execution time limit from inside a script. See the official PHP documentation for set_time_limit() for additional details about how this function behaves.

The function accepts the number of seconds the script is allowed to run:

<?php

set_time_limit(60);

echo "Script can run for up to 60 seconds.";

?>

The above code allows the script to run for an additional 60 seconds.

A value of 0 removes the time limit:

<?php

set_time_limit(0);

?>

Although unlimited execution time can be useful for command-line scripts or controlled background tasks, it should be used carefully on web applications. A slow script without a timeout can hold server resources for an unexpectedly long time.

Changing PHP execution time limit using ini_set()

You can also modify the execution time limit at runtime using ini_set().

<?php

ini_set('max_execution_time', 60);

echo "Execution time limit updated.";

?>

This changes the setting only for the current script execution.

However, ini_set() works only when the PHP configuration allows this directive to be changed at runtime. Some hosting providers disable certain configuration changes for security reasons.

The max_execution_time directive is documented in the official PHP configuration reference: PHP max_execution_time configuration.

Changing PHP execution time limit using .htaccess

If your server uses Apache, you may be able to change the execution time limit through the .htaccess file.

Add the following line:

php_value max_execution_time 60

This sets the PHP execution time limit to 60 seconds for the directory where the .htaccess file exists.

This approach depends on your hosting configuration. If PHP runs through PHP-FPM or your hosting provider disables PHP value overrides, this method will not work.

If you see an Internal Server Error after adding this setting, remove the line and use another method such as php.ini or your hosting control panel.

Changing PHP execution time limit from the command line

PHP CLI scripts behave slightly differently from web requests. When running PHP from the command line, you can override configuration values directly.

php -d max_execution_time=120 script.php

This runs script.php with a 120-second execution limit.

For long-running tasks such as data migration scripts, scheduled jobs, or batch processing, running PHP from the command line is often a better approach than increasing the web request timeout.

Why does my PHP script still timeout after increasing max_execution_time?

Increasing max_execution_time does not always solve timeout problems. There are multiple timeout layers involved in a typical PHP application.

  • PHP execution limit: Controlled by max_execution_time.
  • Web server timeout: Apache, Nginx, or another server may stop a request earlier.
  • Proxy timeout: Services such as reverse proxies or CDNs may have their own limits.
  • Database timeout: A slow query may fail independently of PHP’s execution limit.

For example, increasing PHP execution time from 30 seconds to 300 seconds does not help if your web server terminates requests after 60 seconds.

When a task can take several minutes, consider moving it to a background process instead of keeping a browser request open.

Common causes of PHP maximum execution time errors

The Maximum execution time exceeded error usually indicates that a script is taking longer than expected. Increasing the limit may hide the symptom, but it is worth checking why the script is slow.

1. Infinite loops

A common reason is an accidental infinite loop.

<?php

$count = 1;

while ($count <= 10) {
    echo $count;
}

?>

The above loop never changes the value of $count, so the condition always remains true. PHP eventually stops the script after reaching the execution time limit.

Always make sure loop conditions can eventually become false.

2. Processing large amounts of data in one request

Importing thousands of records, generating large reports, or processing uploaded files can easily exceed the default execution limit.

Instead of increasing the timeout indefinitely, consider processing data in smaller batches.

For example, process 500 records at a time instead of loading and processing an entire dataset in one request.

3. Slow database queries

A PHP script may appear slow when the actual problem is a database query taking too long.

Before increasing the PHP timeout, check:

  • Missing database indexes
  • Queries returning unnecessary rows
  • Repeated queries inside loops
  • Unoptimized joins

A slow query that takes 90 seconds will still be slow even if PHP allows the script to run for 10 minutes.

4. External API calls

Requests to external services can also increase execution time. If an API server responds slowly, your PHP script waits until the response arrives.

Always configure reasonable connection and response timeouts when calling external services.

Does sleep() count toward PHP execution time?

This is a small detail that surprises many developers.

On non-Windows systems, the time spent in system calls, stream operations, database queries, and some external operations is generally not counted toward the execution time limit. On Windows, measured execution time includes real elapsed time.

The exact behavior depends on the PHP environment and operating system. If you need reliable timeout handling for long-running operations, implement your own application-level timeouts instead of depending only on max_execution_time.

Recommended execution time settings for different scenarios

Scenario Recommended approach
Normal website pages Keep the default limit and optimize slow code.
File upload processing Increase the limit if required and process large files carefully.
Report generation Use background processing for large reports.
Database migration Run as a CLI script with controlled batches.
Cron jobs Use CLI PHP and set appropriate limits.

Best practices when increasing PHP execution time

  • Increase the limit only when there is a clear reason.
  • Do not use unlimited execution time for public web requests.
  • Optimize slow queries and inefficient code before changing configuration.
  • Use background jobs for operations that naturally take a long time.
  • Set application-level limits for external API calls and file processing.

A higher execution limit is sometimes necessary, but it should not become a replacement for fixing slow code. A script that needs 10 minutes to complete may be working correctly, or it may be quietly doing unnecessary work.

Frequently asked questions about PHP execution time limit

What is the default PHP execution time limit?

The default value of max_execution_time is commonly 30 seconds. However, the actual value depends on your PHP installation and hosting provider.

You can check the current value using:

<?php

echo ini_get('max_execution_time');

?>

How do I disable PHP execution time limit?

You can disable the execution time limit by setting it to zero:

<?php

set_time_limit(0);

?>

This removes the PHP time limit for that script. Use it carefully, especially in web applications, because a script that never completes can consume server resources.

What is the difference between max_execution_time and max_input_time?

Both settings control PHP request timing, but they apply to different stages.

  • max_execution_time controls how long PHP can execute the script.
  • max_input_time controls how long PHP spends receiving and processing input data before script execution begins.

For example, a large file upload may be affected by max_input_time before PHP even starts processing the uploaded file.

Can I increase PHP execution time from my PHP code?

Yes. You can use set_time_limit() or ini_set() if your server allows runtime configuration changes.

<?php

set_time_limit(120);

?>

If the hosting environment prevents changing this setting, you must update it through php.ini, server configuration, or your hosting control panel.

Why does changing max_execution_time not work?

There can be several reasons:

  • The wrong php.ini file was modified.
  • The web server was not restarted after the configuration change.
  • The hosting provider does not allow the setting to be changed.
  • Another timeout layer, such as Apache, Nginx, or a proxy, is stopping the request.

Use phpinfo() to confirm which PHP configuration file is active and what value PHP is actually using.

Conclusion

The PHP execution time limit protects your server from scripts that run longer than expected. The max_execution_time setting can be changed using php.ini, set_time_limit(), ini_set(), or server configuration depending on your environment.

The ini_set() function allows runtime configuration changes for supported directives. Refer to the official ini_set() documentation for details.

For occasional long-running tasks, increasing the limit is fine. For regular operations such as large imports, reports, or data processing, a better solution is usually to redesign the task using batches or background processing.

Understanding why a script reaches the execution limit will help you fix the actual performance problem instead of simply giving the script more time to run.

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 22, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

10 Comments on "Execution Time Limit in PHP: max_execution_time Explained"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?