• 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
  • Ruby
  • 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.  php array_walk() – Modify Elements in Array with Callback Function
  • 2.  Replace Space with Dash in php String
  • 3.  Remove First Character from String Using php
  • 4.  How to Check If String Contains Substring in PHP
  • 5.  php Print Array – How to Print All Elements in Array
  • 6.  php array_slice() – Get Slice of Elements from Array
  • 7.  Using strtolower() in php to Convert String to Lowercase
  • 8.  Get Current Location Latitude Longitude in php
  • 9.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 10.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy