Execution Time Limit in PHP

In PHP, the default time for executing a program is 30 seconds. This will be set in php.ini file and the directive corresponding to this configuration setting is named as max_execution_time. The value of this directive is expected to be in seconds. If it is 0, meaning that, there is an infinite time limit to allow a PHP program to run.

For CLI SAPI which is for creating shell program in PHP, this directive is set to 0. Because, there is no limit for the execution time while running PHP script using CLI.

Ways to Change PHP program Execution Time

There are various ways to change the value of this directive that is set with the PHP configuration file. These are,

1. Use PHP function set_time_limit() that accepts execution time limit in seconds as its argument.
2. Search the php.ini file for a max_execution_time directive and edit its default value as required.
3. Use ini_set() function in a PHP program by specifying php.ini file directive name and its corresponding value to be set.
4. Use php_value command in .htaccess file to set max_execution_time.


This function is used to set the number of seconds a PHP program is allowed to be executed with a browser. For that, it accepts an integer value as an argument. For example, let us see the following line to explain further.

$execution_time_limit = 45;

We should paste the above two lines in our PHP program where it is required to change the configuration directive at run time. If use this function at the very beginning before we start another coding, then the value specified with this function will be the limit for program execution.

Otherwise, if we set this limit after some lines of code, then the execution time will be extended by this value with the number of seconds taken for the partial execution of the program.

To be very clear, let us consider set_time_limit is used in mid of the program with the value of 40 seconds. And the time taken to execute the code before invoking set_time_limit() is 20 seconds. Then, set_time_limit(40) allow the remaining execution to take the maximum limit of 40 seconds. And totally, the program has 60 seconds as its execution time limit.

Edit max_execution_time Directive

Instead of setting limits for PHP program execution at run time, we can directly change the corresponding directive value that is set with the PHP configuration file php.ini. But, this is possible, if we have access to modify this file, otherwise, we need to raise a manual request to whomsoever maintaining it.

If we have access to this file then we can see the settings related to max_execution_time, like.

; Maximum execution time of each script, in seconds
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30

And then we should replace the default value as required. To make the change with the config file to be effective, we need to restart the server before executing any PHP program.


This is another way to set execution time limit from PHP program as like as set_time_limit() function. This function accepts two arguments, one is the directive name and the value for other. For example, this limit is set by this function using the following line.


The overall execution limit value for a PHP program depends on the position where we invoke ini_set() function in our program. The same calculation we have seen for example while seeing set_time_limit() is applicable to this function also.

Use php_value command in .htaccess

We can change the value of execution time limit with the .htaccess file by using the php_value command as like as follows.

php_value max_execution_time 200


  • All the above method for setting maximum execution limit are used for long running PHP program, like a program that uploads huge size files or on sending mail to multiple recipients and etc.
  • While using PHP functions like set_time_limit() and ini_set(), we need to ensure that the program is not running in safe mode. If so, these function will not create expected results.
  • And, if the time set by these above methods are elapsed the following PHP error will be sent to the browser.

Fatal error: Maximum execution time of ... seconds exceeded

noidatut course

Leave Comment