Catching the "Maximum Execution Time" Error in PHP
The PHP fatal error "Maximum execution time of 30 seconds exceeded" occurs when a script consumes more time than the server's configured maximum execution time limit. While increasing this limit is often recommended, it is also vital to handle the error gracefully.
Catching the Error
PHP does not provide a built-in exception for handling this error. However, the following workaround can be employed:
Example:
<code class="php">function shutdown() { $a = error_get_last(); if ($a == null) { echo "No errors"; } else { echo "Error: " . $a['message'] . "\n"; echo "Type: " . $a['type'] . "\n"; } } register_shutdown_function('shutdown'); ini_set('max_execution_time', 1); // Set a low execution time limit for testing sleep(3); // Simulate a long-running task</code>
In this example, we have registered a shutdown function named shutdown. If no error has occurred, it will print "No errors." If an error has occurred, it will display the error message and type.
Additional Resources:
The above is the detailed content of How to Catch the 'Maximum Execution Time' Error Gracefully in PHP?. For more information, please follow other related articles on the PHP Chinese website!