So, this function will execute the callback function $fn after a specified period of time in seconds. But that’s not the topic here.Īnyways, we will still mimic setTimeout() using the sleep() function, but always keep in mind that it is drastically different from the async setTimeout() in Javascript. However, some libraries feature an event loop for PHP, similar to what we have in Javascript. So sleep()function can be thought of as a blocking timer, and since PHP is not event-driven and thus non-blocking timers are a rare sight. So, it is generally not a bright idea to block a thread unless there is some use case where you actually have to. In contrast, the PHP sleep() function is not an asynchronous function therefore, it blocks the thread of execution. Meanwhile, it doesn’t block the thread and continues executing the rest of the script. It is asynchronous in nature and delays the execution of a function for a specified period of time. If you’re coming from Javascript then setTimeout() is perhaps a known function to you. SetTimeout() Example using PHP Sleep() Function Let’s see a basic example of how to pause a script using the PHP sleep()function. Using the PHP sleep() Function to Pause Running Code Example This function returns a ValueError if the specified seconds are less than 0. Seconds – Halt time in seconds (Expects an integer greater than or equal to 0) Errors/Exceptions
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |