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
19
Related Errors
Database
PDOException (Database Connection)
PHP failed to connect to or communicate with the database server.
PHP
Cannot Use String Offset as Array
The code tries to access a string using array syntax, treating it as an array.
Database
Deadlock Found
Two or more database transactions are blocking each other, creating a circular wait.
500
Laravel
Laravel 500 Internal Server Error
A generic server-side error indicating something went wrong during request processing.