search
php star Featured

Fix: Maximum execution time exceeded error in PHP

Quick guide to fix 'Maximum execution time exceeded' errors in PHP. Essential fixes with minimal code examples.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 4 min read
PHP Execution Time Timeout Performance Optimization Code Fixes

The ‘Maximum execution time exceeded’ error occurs when a PHP script runs longer than the allowed time limit. This error stops script execution and must be resolved for proper functionality.


Common Causes and Fixes

1. Increase Time Limit

<?php
// ❌ Error: Script times out after 30 seconds
sleep(40);  // Error!
?>
<?php
// ✅ Fixed: Increase time limit
ini_set('max_execution_time', 60);  // 60 seconds
sleep(40);  // OK
?>

2. Infinite Loop

<?php
// ❌ Error: Infinite loop
while (true) {
    echo "Looping...";
}
?>
<?php
// ✅ Fixed: Proper loop condition
$count = 0;
while ($count < 10) {
    echo "Looping... $count";
    $count++;
}
?>

3. Large File Processing

<?php
// ❌ Error: Processing large file
$contents = file_get_contents('huge_file.txt');
$processed = strtoupper($contents);  // May timeout
?>
<?php
// ✅ Fixed: Process in chunks
$handle = fopen('huge_file.txt', 'r');
while (!feof($handle)) {
    $chunk = fread($handle, 8192);  // 8KB chunks
    $processed = strtoupper($chunk);
    // Process chunk
}
fclose($handle);
?>

4. Recursive Function Without Termination

<?php
// ❌ Error: Infinite recursion
function factorial($n) {
    return $n * factorial($n - 1);  // No base case
}
?>
<?php
// ✅ Fixed: Proper termination
function factorial($n) {
    if ($n <= 1) return 1;  // Base case
    return $n * factorial($n - 1);
}
?>

5. Database Query Timeout

<?php
// ❌ Error: Slow query
$stmt = $pdo->prepare("SELECT * FROM huge_table");
$stmt->execute();  // May timeout
$results = $stmt->fetchAll();
?>
<?php
// ✅ Fixed: Use pagination
$stmt = $pdo->prepare("SELECT * FROM huge_table LIMIT 1000");
$stmt->execute();
$results = $stmt->fetchAll();
?>

6. Unnecessary Processing

<?php
// ❌ Error: Heavy computation
for ($i = 0; $i < 10000000; $i++) {
    $result = expensive_calculation($i);  // Too slow
}
?>
<?php
// ✅ Fixed: Optimize or batch
for ($i = 0; $i < 1000000; $i += 1000) {
    $batch = array_slice(range($i, $i + 999), 0, 1000);
    foreach ($batch as $item) {
        $result = quick_calculation($item);
    }
}
?>

7. File Upload Processing

<?php
// ❌ Error: Processing large upload
if ($_FILES['file']['size'] > 10000000) {  // 10MB
    process_large_file($_FILES['file']['tmp_name']);  // May timeout
}
?>
<?php
// ✅ Fixed: Validate size first
if ($_FILES['file']['size'] > 10000000) {  // 10MB
    die("File too large");
}
process_file($_FILES['file']['tmp_name']);
?>

8. Using set_time_limit()

<?php
// ❌ Error: Default timeout
for ($i = 0; $i < 1000000; $i++) {
    process_item($i);
}
?>
<?php
// ✅ Fixed: Set time limit
set_time_limit(60);  // 60 seconds
for ($i = 0; $i < 1000000; $i++) {
    process_item($i);
}
?>

9. Progress Monitoring

<?php
// ❌ Error: No timeout handling
foreach ($large_array as $item) {
    process_item($item);
}
?>
<?php
// ✅ Fixed: Monitor and reset timer
$processed = 0;
foreach ($large_array as $item) {
    process_item($item);
    $processed++;
    
    if ($processed % 1000 === 0) {
        set_time_limit(30);  // Reset timer every 1000 items
    }
}
?>

10. Background Processing

<?php
// ❌ Error: Long process in web request
process_long_task();  // May timeout
echo "Done";
?>
<?php
// ✅ Fixed: Queue for background
queue_task('long_process');
echo "Task queued";
?>

Quick Debugging Steps

  1. Check script execution time with microtime()
  2. Identify slow operations (loops, queries, file processing)
  3. Verify time limit with ini_get('max_execution_time')
  4. Use profiling tools to find bottlenecks
  5. Test with smaller datasets

Prevention Tips

  • Set appropriate time limits: ini_set('max_execution_time', 30)
  • Use set_time_limit(30) to reset timer
  • Process large datasets in chunks
  • Optimize database queries
  • Avoid infinite loops and recursion
  • Validate input size before processing
  • Use background jobs for long tasks
  • Monitor script performance

Remember: Increase execution time only as a temporary solution. Optimize code for better performance.

Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

php

Fix: Fatal error: Allowed memory size exhausted error in PHP

Learn how to fix the 'Fatal error: Allowed memory size exhausted' error in PHP. Complete guide with solutions, optimization techniques, and memory management best practices.

January 8, 2026
php

How to Fix: MySQL server has gone away error in PHP

Quick guide to fix 'MySQL server has gone away' errors in PHP. Essential fixes with minimal code examples.

January 8, 2026
php

Fix: Call to a member function prepare() on bool error in PHP - Quick Solutions

Quick guide to fix 'Call to a member function prepare() on bool' errors in PHP. Essential fixes with minimal code examples.

January 8, 2026