Detecting a timeout for a block of code in PHP
问题 Is there a way you can abort a block of code if it's taking too long in PHP? Perhaps something like: //Set the max time to 2 seconds $time = new TimeOut(2); $time->startTime(); sleep(3) $time->endTime(); if ($time->timeExpired()){ echo 'This function took too long to execute and was aborted.'; } It doesn't have to be exactly like above, but are there any native PHP functions or classes that do something like this? Edit : Ben Lee's answer with pcnt_fork would be the perfect solution except