Home >Backend Development >PHP Tutorial >Detailed explanation of php set_time_limit() usage test_PHP tutorial
The set_time_limit function in php is used to limit the execution time of the page. For example, if I want to define the execution time of a php page as 5 seconds, I can just set_time_limit(5).
A php script is executed every 5 minutes through crontab. Considering that the script execution time will exceed 5 minutes, set_time_limit(290) is specially used to control the script to exit in 290 seconds. One day, I suddenly discovered that there were multiple processes executing the script in the background, which meant that set_time_limit(290) did not work. In order to prove, the following code test is specially used.
The code is as follows | Copy code | ||||||||
for ($i = 0; $i < 100; $i++) { echo date('Y-m-d H:i:s') . "n"; sleep(1); }
|
Whether it is under web or CLI, the above script does not exit after 5 seconds. Later, the ini_set(‘max_execution_time’, 5) test was added, and the results were the same. Does that mean that the set_time_limit function is useless at all? In fact, this is not the case. The root cause is found here http://stackoverflow.com/questions/5874950/set-max-execution-time-in-php-cli. In fact, there is a problem with the above writing method. For example, use the following code:
The code is as follows | Copy code |
set_time_limit(5); for (;;) { |
After execution, you will see an error message like "Fatal error: Maximum execution time of 5 seconds exceeded in" in about 5 seconds. It shows that set_time_limit works. Now I am going to take a look at the description of this function in the official document (http://www.php.net/manual/en/function.set-time-limit.php). I wrote in Note:
代码如下 | 复制代码 |
//set_time_limit(0); $i=1500; include ("inc/conn.php"); while($i>0) { $sql="INSERT INTO php (php) VALUES ('$i')"; if ($conn->execute($sql)===flase) { //echo "数据插入错误".$conn->errormsg(); } else { $phpid=$conn->Insert_ID(); echo $i."已经存入数据库,编号:".$phpid; } $i--; echo " "; } ?> |
Note: The pause time of the sleep function is not included in the execution time of the script. So that's why the first test failed.
When your page has a large amount of data, it is recommended to use set_time_limit() to control the running time. The default is 30s, so you need to lengthen the execution time, such as set_time_limit(300), where the number of seconds is set to 0, which means Keep running!
For example: set_time_limit(0) means that the link is running for a long time!
Note: The operation of this function requires you to turn off the safe mode. Set safe_mode = Off in php.ini and set the safe mode to Off, otherwise the following error will occur:
Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode in
Note again:
In php.ini, you can set the maximum execution time of the PHP page by defining max_execution_time, such as the following:
代码如下 | 复制代码 |
set_time_limit(900); |
This function specifies the maximum execution time of the current php script,
Although the setting value is 900 seconds, in fact
Maximum execution time = max_execution_time value in php.ini - the time the current script has been executed + setting value
If max_execution_time=30 in php.ini and the current script has been executed for 10 seconds, then:
Maximum execution time = 30-10+900 = 920 seconds.
Solution to setting set_time_limit in php not working:
set_time_limit is used to set the timeout of the script. The usage is as follows:
set_time_limit(seconds);
It stipulates that the program must run to completion within the specified number of seconds from the time this sentence is run,
If the timeout occurs, the program will exit with an error.
But sometimes setting set_time_limit has no effect. The set_time_limit function is best executed under Linux, and it may not be effective when executed under Windows
Solution:
Modify max_execution_time = 30 in php.ini. The default is 30 seconds, change it to max_execution_time = 300. Restart the apache server. In this way, if the timeout is set to 300 seconds, a prompt message will appear.