How to Set Maximum Execution Time in PHP
-
Use the
ini_set()
Function to Set the Maximum Execution Time of a Script in PHP -
Use the
set_time_limit()
Function to Set Maximum Execution Time of a Script in PHP -
Change the
max_execution_time
Option inphp.ini
to Set Maximum Execution Time of a Script in PHP
In PHP, the execution timeouts after a certain period are generally thirty seconds. So, it is a hassle to process a huge file.
But, we can set the maximum execution time of the script by ourselves. This article will introduce three ways of setting the maximum execution time of a script in PHP so that big files will be processed easily.
Use the ini_set()
Function to Set the Maximum Execution Time of a Script in PHP
We can use the ini_set()
function to set the execution time of a PHP script. The function is used to set the value of PHP configurations.
The first parameter takes the configuration name, while the second parameter is the value for the configuration. The configuration name should be a string.
PHP has a configuration max_execution_time
responsible for defining the script’s maximum execution time. We can use the configuration option in the ini_set()
function to set our desired execution time.
For example, write the ini_set()
function at the top of the PHP script. Write 'max_execution_time'
as the first parameter and '240'
as the second parameter.
Consequently, the execution time has been set to 240 seconds, equivalent to four minutes. We can also set the second parameter to '0'
to set the execution time to infinite.
Thus, we can use the ini_set()
function to set the execution time of the current PHP script.
Example Code:
ini_set('max_execution_time', '240');
Use the set_time_limit()
Function to Set Maximum Execution Time of a Script in PHP
We can also set the execution time of a PHP script using the set_time_limit()
function.
The function sets the maximum execution time of a script. It takes an integer parameter in seconds.
For example, write the function set_time_limit()
and the integer 240
as its parameter. As a result, the maximum execution time of the current PHP script is set to four minutes.
However, we need to turn the safe_mode
configuration option off
in php.ini
to use the function. It is because set_time_limit()
can potentially compromise the server security.
For example, if we set set_time_limit(0)
, an infinite loop can run forever in the script as there is no bound to maximum execution time. To turn safe_mode
off, we can add the line safe_mode = Off
in the php.ini
file.
We can set the script’s maximum execution time in PHP using the set_time_limit()
function.
Example Code:
set_time_limit(240);
Change the max_execution_time
Option in php.ini
to Set Maximum Execution Time of a Script in PHP
The methods we discussed above set the maximum execution time of the script temporarily. That is, when the script ends, the configuration is also terminated.
But, there is a way of permanently setting the maximum execution time of all the PHP scripts. We need to change the configuration option max_execution_time
with our desired value in the php.ini
file.
We can locate the php.ini
file by navigating the output of the phpinfo()
function.
The example below sets the maximum execution time to 240
, equivalent to four minutes.
Example Code:
max_execution_time = 240
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn