Using PHP Sleep Like Javascript setTimeout: 2023 Code Examples

How to Use the Sleep() function in PHP PHP sleep() halts a running script for specific seconds. It is a synchronous and blocking timer that may surprise you if you switch from an async context. Let’s quickly go through some highlights and clarify some presumptions.. PHP Sleep Function Code Example <?php /* @params $fn - callback function $seconds - halt time in seconds (expects an integer greater than or equal to zero) */ function setTimeout($fn, $seconds){ sleep($seconds); $fn(); } $greet = function() { echo 'Hello' . "\n"; }; echo "Before calling setTimeout() ".date('h:i:s') . "\n"; setTimeout($greet, 5); echo "After timeout ".date('h:i:s') . "\n"; /* Before calling setTimeout() 07:44:18 Hello After timeout 07:44:23 */ ?> Article Highlights An asynchronous & non-blocking timer doesn’t block a thread but puts off the execution of a function for a defined timeout duration. PHP sleep() function is not an asynchronous function; therefore, it blocks the…

read more

Using while vs foreach loop Correctly: 5 PHP Code Examples (2023)

when to use while vs foreach loop in PHP
The while and foreach loop are among the several loops in PHP. A loop executes a block of code repeatedly. Then why do we have all these different types if they do the same job? That’s a good question, and the answer is that while they inherently do the same job, some loops fit well for a particular use case.  This statement might sound a little abstract at this point. This article will explore when to use while vs foreach loop in PHP. By the end of this article, you’ll understand how one loop fits a specific job better than the other. So, let’s move on to the article. When to use while vs foreach loop in PHP - while loop  We’ve already seen details of the while loop in one of the articles. Here’s a review. Description While loop checks a boolean condition on entry. If the condition is…

read more

Difference between while & do-while loops: 3 PHP Code Examples (2023)

Difference between while and do while loops in PHP
Loops are fundamental building blocks in programming. You will use them quite often to execute a statement repeatedly. A loop is controlled by a boolean condition, and it continues to run over and over as long as the condition is true. The most common usage of loops is iterating over arrays or objects. We’ve seen loops in the same context in many of the tutorials. There are different kinds of loops in PHP, namely, for, foreach, while, and do while.Some starters are often confused about while and do while because they sound sort of similar. However, they work differently. So in this tutorial, we’ll see the difference between while and do while loops in PHP. Difference between while and do while loops in PHP - While Loop While loop checks a boolean condition on entry. If the condition is true, it executes the body. After the first run, it rechecks…

read more

PHP serialize vs json_encode

php serialize vs json_encode
Data formats are interchangeable and this allows you to move back and forth between different data formats. This data transformation is usually necessary for data storage, transfer, and communication over a network in computers. The two most renowned data formats are JSON, XML, and CSV. JSON is quite common these days as RESTful APIs use JSON, thus many applications use JSON data to communicate with servers. The image below shows a glimpse of a computer communicating with a server using JSON data. Many programming languages feature a built-in serialization interface that provides features for data transformation, usually specific to a programming language. Unlike JSON, XML, and CSV, serialized data is in the form of an encoded text, as we’ll see later.  In this article about PHP serialize vs json_encode, we’ll explore serialization in PHP. We’ll see serialize function and compare it to json_encode. So, let’s move forward without any further…

read more