Enhance Your PHP Code: Understanding sleep() Method

 How to delay program execution using sleep() function in PHP

Enhance-Your-PHP-Code-Understanding-sleep()-Method

PHP provides lots of functions to make our coding experience more faster and efficient, There are some scenarios and requirements where we need to halt/delay our execution for some perticular fraction of seconds. In such scenarios sleep() functions helps us to achieve this.

sleep('amount of time in seconds') function basically delays our program execution for a perticular fraction of seconds, on success it returns zero and on error it returns false. 

Example :-

public function useSleep(){
        try{
            // current time
            echo date('h:i:s') . "\n";

            // sleep for 10 seconds
            sleep(10);

            // wake up !
            echo date('h:i:s');
        }
        catch(\Exception $e){
            return false;
        }
    }




Previous Post Next Post

Contact Form