Fatal error: Maximum execution time exceeded
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
This error means your PHP script took longer to run than PHP allows. The default time limit is 30 seconds. PHP kills the script when it exceeds that limit to protect the server from runaway processes. This usually points to an infinite loop, a very slow database query, or a long-running task that needs to be handled differently.
Affected Models
- PHP 5.x
- PHP 7.x
- PHP 8.x
- All PHP versions
Common Causes
- An infinite loop — a while or for loop with a condition that never becomes false
- A very slow or unoptimized database query that takes too long to return results
- Fetching data from an external URL or API that is slow or not responding
- Processing a very large file line by line with no time optimization
- A recursive function that has no proper exit condition
How to Fix It
-
To quickly bypass the limit for testing, add this at the top of your script: set_time_limit(120); — this sets the limit to 120 seconds.
Use this only during development and testing. Do not rely on it as a permanent fix.
-
Check your code for any loop that might never end. Make sure every loop has a condition that will eventually become false.
Add a counter or a safety maximum iteration count to any loop processing large data.
-
Optimize slow database queries. Add indexes to columns used in WHERE, JOIN, and ORDER BY clauses.
A query without an index on a large table can take minutes. The same query with an index takes milliseconds.
-
When calling external APIs or URLs, always set a timeout: use curl_setopt($ch, CURLOPT_TIMEOUT, 10); to limit the wait to 10 seconds.
Without a timeout, your script will wait forever for an API that never responds.
-
For genuinely long tasks — like importing 10,000 records — split the work into smaller batches and process them over multiple requests or using a scheduled job.
This is the right architectural solution for tasks that legitimately take more than 30 seconds.
When to Call a Professional
If you are regularly hitting the execution time limit in a production application, the problem likely needs a developer. Long-running tasks should be moved to background jobs or queues. A PHP or backend developer can help restructure the task so it does not block the web request.
Frequently Asked Questions
Is it safe to increase the PHP execution time limit?
For one-off scripts like data imports, yes. For web pages that users visit, no. A page that takes more than 5–10 seconds to load is a bad user experience. If your page needs more time, restructure the task to run in the background.
Where is the default time limit set?
In php.ini, look for: max_execution_time = 30 You can change it there, or use set_time_limit() in your code. Some shared hosts set a lower limit (10 seconds) that you cannot override. Contact your host if needed.
Does the time limit include waiting for database queries or API calls?
Yes — the clock runs from the moment the script starts. Every second spent waiting for a database or API counts toward the limit. This is why optimizing queries and setting API timeouts is so important.