PHP endless loop not completed after 60 seconds
I created an endless PHP while loop that incremented a variable and then echoed:
$num = 1;
while($num >0) {
echo $num . "<br/>";
$num++;
}
I expected this to be killed / completed after 60 seconds since the settings in php.ini are as follows:
max_execution_time 60 60
max_input_time 60
Sorry if I'm wrong, but I was expecting to see the job killed in the browser (no new echoes!) ...
Can anyone give me more information on endless PHP jobs run and when they are actually killed on the server?
source to share
You are confusing runtime with wall clocks. They are not the same thing. The processor uses very little execution time for each cycle of your code. This will eventually lead to an exit, but it will take much more than a minute.
Think of it this way, your processor can run at 2 GHz. How many instructions do you think to do one of your loops? The echo time is long (i.e. slow) and does not take into account the CPU runtime.
source to share
//using set_time_limit
// starting php code here
echo "starting...\n";
// set_time_limit(10); //either would work
ini_set("max_execution_time", 10); //either would work
function doSomeExpensiveWork($currentTime){
for ($r = 0; $r < 100000; $r++){
$x = tan(M_LNPI+log(ceil( date("s")*M_PI*M_LNPI+100)));
}
}
try{
while(true)
{
$currentTime = date("H:m:s");
echo $currentTime, "\n";
doSomeExpensiveWork($currentTime);
}
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "this will not be executed! $x";
// code end
source to share