0

I'm triyng to catch timeout error to output some clear text to the user (like "Sorry, timeout"). So why does this example:

function shutdown() { 
    $a=error_get_last(); 
    if($a==null)   
        echo "No errors"; 
    else 
         print_r($a); 

} 
register_shutdown_function('shutdown'); 
ini_set('max_execution_time',1 ); 
sleep(3); 

output no errors?? I'm confused about it. Here this example looks helpful. Thanks

0

1 Answer 1

3

Try not using sleep(), seems to work if the reason for timeout is real work:

Example

function isPrime($num) {
    if($num == 1)
        return false;
    if($num == 2)
        return true;
    if($num % 2 == 0) {
        return false;
    }
    for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
        if($num % $i == 0)
            return false;
    }
    return true;
}
function shutdown() 
{ 
     $a=error_get_last(); 
     if($a==null)   
         echo "No errors"; 
     else 
          print_r($a); 
} 
register_shutdown_function('shutdown'); 
ini_set('max_execution_time',1 ); 
$ps = 0;
for ($i = 0; $i < 1000000; $i++) {
    if (isPrime($i)){
        $ps++;
    }
}
echo $ps;
Sign up to request clarification or add additional context in comments.

1 Comment

Looks useful for me. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.