How can we set infinite execution time in a PHP script?
Add set_time_limit(0) at the beginning of the script. Zero means no time limit. You can also use ini_set("max_execution_time", 0).
<?php
set_time_limit(0); // no time limit for this script
// long running job here
?>
- It works only if your hosting allows it. Some hosts disable
set_time_limit()or force a limit. - The web server can still stop the request: Apache has
Timeout, Nginx hasfastcgi_read_timeout, and some hosts have a proxy limit. - In the command line (CLI) the default is already 0, so a long script runs without limit.
Note:
A web page that runs for many minutes is a bad idea, because the visitor is waiting and can close the page. Run long jobs in the background (a cron job or a queue) and show the progress. To only increase the limit, see how to increase maximum execution time.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Errors |
| Exercises & Assignments |
|---|
| No Content Found. |