• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php sleep() Function – Pause Execution of Code in php

php sleep() Function – Pause Execution of Code in php

April 6, 2022 Leave a Comment

The php sleep() function gives us the ability to pause the execution of our code. sleep() delays the execution of code by a given number of seconds.

sleep(30); // program sleeps for 30 seconds

When programming in php, sometimes we want to delay the execution of a piece of code so that other processes can catch up or complete.

We can use the php sleep() function to delay the execution of code in php.

sleep() takes one parameter which is the number of seconds you want to sleep your code.

Below is an example of how to use sleep() in php.

echo date("Y-m-d H:i:s") . "\n"; 

sleep(30); // program sleeps for 30 seconds

echo date("Y-m-d H:i:s") . "\n";

// Output: 
2022-04-06 08:11:27
2022-04-06 08:11:57

Using usleep() function in php to Delay Execution by Microseconds

If you need to sleep the code for less than a second, or want to use fractions of a second, then you can use the php usleep() function.

The usleep() function has the same behavior as sleep() but instead of seconds, we pass microseconds.

Below is an example of how to use usleep() to pause the execution of your code in php.

echo date("Y-m-d H:i:s") . "\n"; 

usleep(1000000); // program sleeps for 1 seconds

echo date("Y-m-d H:i:s") . "\n";

// Output: 
2022-04-06 08:12:27
2022-04-06 08:12:28

Hopefully this article has been useful for you to understand how to use the php sleep() function.

Other Articles You'll Also Like:

  • 1.  What is the Difference Between unset() and unlink() in php?
  • 2.  php array_pop() – Remove Last Element from Array
  • 3.  php acos – Find Arccosine and Inverse Cosine of Number
  • 4.  Using php to Copy Files Easily from One Directory to Another
  • 5.  How to Parse URLs with php parse_url() Function
  • 6.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 7.  php max – Find Maximum Value of Array in php
  • 8.  How to Create an Empty Array in php
  • 9.  How to Add Items to Array in php
  • 10.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy