My PHP Script Is Timing Out

Updated 25 February 2026 10550 views Troubleshooting

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

  1. Increase max_execution_time – In cPanel, go to MultiPHP INI Editor, select your domain, and increase max_execution_time to 60 or 120 seconds. Alternatively, add ini_set('max_execution_time', 120); at the top of your script.
  2. Optimise database queries – Add indexes to frequently queried columns. Use EXPLAIN in phpMyAdmin to analyse slow queries and rewrite them for efficiency.
  3. 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);
  4. Process data in batches – Instead of processing thousands of records at once, break the work into smaller chunks using pagination or LIMIT clauses.
  5. Use background processing – For long-running tasks, consider using a cron job instead of running the task during a web request.
  6. Check for infinite loops – Review your code for while or for loops 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.

Was this article helpful?

Let us know so we can improve our docs.