Search code examples
phpphp-5.3

Can I set a time limit for a code block?


Is it possible to start a block of code (maybe just call a function) and if it doesn't execute within a certain time skip it.

//give this function 10 seconds to execute
$value = mega_function();// could take anything from 1-1000 seconds
//if 10 seconds have passed and the value is still not set, abort it and set $value = false;

Solution

  • No. You would have to either

    • Call the function inside an external file using curl or file_get_contents() - you can set a timeout there

    • Keep track of the time inside mega_function() and return() if necessary.

    What does mega_function() do?