How to force a maximum execution time error in PHP?
To test this error, set a small time limit with set_time_limit() and run a loop that keeps PHP busy, so the script crosses the limit.
<?php
set_time_limit(2); // allow only 2 seconds
while (true) {
// busy loop, never stops
}
?>
Output:
Sample message, the path will be different
Fatal error: Maximum execution time of 2 seconds exceeded in /path/index.php on line 5
Note:
Many people try sleep(10), but on Linux and macOS the execution time counts only the time PHP really works (CPU time), not the time it sleeps. So sleep() will not give this error there. On Windows the sleeping time is counted.
To remove the limit, see how to set infinite execution time, and to change the default see how to increase maximum execution time.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Errors |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| What are the different types of error in PHP? |
| How to force a maximum execution time error in PHP? |