Skip to content
PHP Logo

What potential issue arises when using the sleep() function in PHP for creating a timer?

The potential issue that arises when using the sleep() function in PHP for creating a timer is that it blocks the entire script execution, causing delays in other processes. To solve this issue, you can use the usleep() function instead, which allows for microsecond delays without blocking the script.

php
<?php
// Function to create a timer using usleep
function timer($seconds) {
    $microseconds = $seconds * 1000000;
    usleep($microseconds);
    echo "Timer finished after $seconds seconds.";
}

// Set timer for 3 seconds
timer(3);
?>