In PHP, the default time for executing 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 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.
There are various ways to change the value of this directive that is set with the PHP configuration file. These are,
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; set_time_limit($execution_time_limit);
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 other coding, then the value specified with this function will be 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 maximum limit of 40 seconds. And totally, the program has 60 seconds as its execution time limit.
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 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 ; http://php.net/max-execution-time ; 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 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 argument, one is the directive name and the value for other. For example, this limit is set by this function using following line.
The overall execution limit value for a PHP program is 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.
We can change the value of execution time limit with .htaccess file by using php_value command as like as follows.
php_value max_execution_time 200
Fatal error: Maximum execution time of ... seconds exceeded
This PHP code tutorial was published on June 19, 2013.