Understanding PHP Timeouts
A PHP timeout occurs when a script takes longer to execute than the server's configured maximum execution time. By default, most hosting environments set this to 30 seconds. When a script exceeds this limit, PHP terminates it and returns a fatal error.
Common Causes
- Complex database queries – Unoptimised queries scanning large tables without proper indexes can take a very long time.
- External API calls – If your script calls an external service that is slow to respond, the wait time counts toward your execution limit.
- Large file processing – Importing large CSV files, processing images, or generating PDF documents can exceed the time limit.
- Infinite loops – A bug in your code creating an infinite loop will always result in a timeout.
- Resource-intensive operations – Running backups, bulk email sends, or intensive calculations within a single request.
How to Fix It
- Increase max_execution_time – In cPanel, go to MultiPHP INI Editor, select your domain, and increase
max_execution_timeto 60 or 120 seconds. Alternatively, addini_set('max_execution_time', 120);at the top of your script. - Optimise database queries – Add indexes to frequently queried columns. Use
EXPLAINin phpMyAdmin to analyse slow queries and rewrite them for efficiency. - Set timeouts on API calls – When using cURL or file_get_contents, set a timeout so your script does not wait indefinitely:
curl_setopt($ch, CURLOPT_TIMEOUT, 10); - Process data in batches – Instead of processing thousands of records at once, break the work into smaller chunks using pagination or LIMIT clauses.
- Use background processing – For long-running tasks, consider using a cron job instead of running the task during a web request.
- Check for infinite loops – Review your code for
whileorforloops that may not have a proper exit condition.
If you need a higher execution time for a specific use case, contact support to discuss the best approach for your hosting plan.