Name

set_time_limit()

Synopsis

    void set_time_limit ( int seconds )

The set_time_limit() function lets you set how long a script should be allowed to execute. This value is usually set inside php.ini under the max_execution_time setting; however, you can override that here. The function takes one parameter, which is the number of seconds you want the script to have. Or you can pass 0, which means "Let the script run as long as it needs." This example sets the script execution time to 30 seconds:

    set_time_limit(30);

When you use this function, the script timer is reset to 0; if you set 50 as the time limit, then after 40 seconds set the time limit to 30, the script will run for 70 seconds in total. That said, most web servers have their own time limit over and above PHP's. In Apache, this is set under Timeout in httpd.conf, and defaults to 300 seconds. If you use set_time_limit() to a value greater than Apache's timeout value, Apache will stop PHP before PHP stops itself. PHP may let some scripts go over the time limit if control is outside the script. For example, if you run an external program that takes 100 seconds and you have set the time limit to 30 seconds, PHP will let the script carry on for the full 100 seconds and terminate immediately afterwards. This also happens if you use the sleep() function with a value larger than the amount of time the script has left to execute.

Tip

The script time limit specified in php.ini or using set_time_limit() is also used to specify the number ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.