Max execution time
The maximum execution time is one of the most common errors developers come across. By default, the maximum execution time of the PHP script executing in the browser is 30 seconds, unless we execute the script within the CLI environment, where there is no such limitation.
We could easily test that through a simple example, given through the index.php
and script.php
files, as follows:
<?php // index.php require_once 'script.php'; error_reporting(E_ALL); ini_set('display_errors', 'On'); sleep(10); echo 'Test#1';
?php // script.php sleep(25); echo 'Test#2';
Executed from within the browser, this will return the following error:
Test#2 Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/index.php on line 5
Executed from within the CLI environment, this will return the following output:
Test#2Test#1
Luckily for us, PHP provides two ways to control the timeout value:
- Using the
max_execution_time
configuration directive (php.ini
file,ini_set()
function) - Using...