Maximum Execution Time Exceeded
A script ran longer than the allowed maximum execution time limit.
Explanation
Maximum execution time exceeded occurs when a PHP script runs longer than the configured max_execution_time setting. The default is typically 30 seconds in PHP configuration. This commonly happens with long-running operations like processing large datasets, making slow API calls, running complex database queries, or processing file uploads without proper optimization. The script is terminated by PHP and an error is logged.
Common Causes
- Processing large datasets without chunking
- Slow external API calls
- Complex database queries without indexes
- File processing without streaming
- Infinite loop in code
Solution
Optimize the code to reduce execution time: add database indexes, batch processing, or pagination. Increase max_execution_time in php.ini or use set_time_limit() at the start of the script. For CLI scripts, the default limit is often 0 (unlimited), but web requests still have limits. Use background job processing for long tasks instead of processing in the HTTP request. Implement chunked processing for large datasets. Use Laravel's chunk() method on queries instead of get() for large result sets.
Code Example
// This can timeout with large datasets
$users = User::all(); // loads millions of records
foreach ($users as $user) {
processUser($user); // slow operation
}
// Fix: use chunking
User::chunk(100, function ($users) {
foreach ($users as $user) {
processUser($user);
}
});
// Or use cursor for memory efficiency
foreach (User::cursor() as $user) {
processUser($user);
}
// For one-off scripts, increase time limit
set_time_limit(120); // 2 minutes
// In php.ini
max_execution_time = 60
Error Information
Language
php
Difficulty
Intermediate
Views
139
Related Errors
Laravel
ReflectionException
A class or method could not be found or instantiated via PHP reflection.
DevOps
Laravel Schedule Task Failure
A scheduled task failed to execute or completed with errors.
PHP
Deprecated Function Usage
The code uses a function that has been deprecated and may be removed in future PHP versions.
PHP
Undefined Variable
The code references a variable that has not been declared or assigned a value.