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.
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.
<?php
$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 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.
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
; 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 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.
<?php
ini_set("max_execution_time", 5);
?>
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.
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
Note:
Fatal error: Maximum execution time of ... seconds exceeded
Hai Vincy! Your Website is awesome for web developing in Php Programmer
Thank you Vadivel for the nice words.