Quick Answer
Handle long-running PHP scripts.
Understanding the Issue
PHP has a default execution time limit (30 seconds) that prevents infinite loops and hung processes.
The Problem
This code demonstrates the issue:
Php
Error
while(true) {
// Infinite loop
}
The Solution
Here's the corrected code:
Php
Fixed
// Solution 1: Increase time limit
set_time_limit(120); // 2 minutes
// Solution 2: Process in chunks
foreach($items as $item) {
process_item($item);
set_time_limit(30); // Reset counter
}
// Solution 3: CLI specific
if (PHP_SAPI === 'cli') {
// No time limit for CLI scripts
set_time_limit(0);
}
// Solution 4: Optimize slow code
// Profile with xdebug to find bottlenecks
Key Takeaways
Set reasonable timeouts and optimize performance-critical sections.