Reader Level:
ARTICLE

Misc Function in PHP: Part 4

Posted by Sharad Gupta Articles | PHP May 06, 2013
In this article I describe the PHP Misc functions sleep, time_nanosleep, time_sleep_until, uniqid, unpack and usleep.
  • 0
  • 0
  • 2971

Introduction

In this article I describe the PHP Miscellaneous functions sleep, time_nanosleep, time_sleep_until, uniqid, unpack and usleep. To learn some other FileSystem functions, go to:

  1. Misc Function in PHP: Part 1
  2. Misc Function in PHP: Part 2
  3. Misc Function in PHP: Part 3

PHP sleep() Function

The PHP Miscellaneous sleep function delays execution of the current script for a specified number of seconds and it returns zero on success or false on error.

Syntax

sleep(seconds)

Parameter in sleep function

The parameter of the function is:

Parameter Description
seconds It specifies the number of seconds to delay the script.

Example

An example of the
function is:

<?
php
print_r
(date('h:i:s'));
sleep
(10);
echo
"</br>";
print_r
(date('h:i:s')); // it prints after 10 seconds time.
?>

Output


sleep-function-in-php.gif

PHP time_nanosleep() Function

The PHP Miscellaneous time_nanosleep function delays execution for a specified number of seconds and nanoseconds and this function returns true on success or false on failure.

Syntax

time_nanosleep(seconds,nanoseconds)

Parameter in time_nanosleep function

The parameters of the function are:

Parameter Description
seconds It specifies the number of seconds to delay the script.
nanoseconds It specifies the number of  nanoseconds to delay the  script.

Example

An example of the
function is:

<?
php
echo
time_nanosleep(2,20000)? 'true':'false';
echo
"</br>";
if
(time_nanosleep(0,50000000)===true)
{

echo
"Slept for a half of a second\n";
}

else

{

echo
"Something went wrong!";
}

?>


Output


time-nano-sleep-function-in-php.gif

PHP time_sleep_until() Function

The PHP Miscellaneous time_sleep_until function puts the script asleep until the specified time and this function returns true on success or false on failure.

Syntax

time_sleep_until(timestamp)

Example

An example of the
function is:

<?
php
var_dump
(time_sleep_until(time()+10));
?>


Output


time-sleep-until-function-in-php.gif

PHP uniqid() Function

The PHP Miscellaneous uniqid function generates a unique id based on the micro time and this function returns the unique identifier, as a string.

Syntax

uniqid(prefix,more_entropy)

Parameter in uniqid function

The parameters of the function are:

Parameter Description
prefix It specifies the prefix of the unique id.
more_entropy It specifies more entropy at the end of the return value.

Example

An example of the
function is:

<?
php
echo
uniqid();
?>

Output

uniq-id-function-in-php.gif 

PHP unpack() Function

The PHP Miscellaneous unpack function unpacks data from a binary string and this function returns an associative array containing unpacked elements of the binary string.

Syntax

unpack(format,data)

Parameter in unpack function

The parameters of the function are:

Parameter Description
format It specifies the format to use when unpacking data and its possible values are:
  • a - It specifies NUL-padded string.
  • A - It specifies SPACE-padded string.
  • c - It specifies signed char.
  • C - It specifies unsigned char.
  • d - It specifies double (machine dependent size and representation).
  • f - It specifies float (machine dependent size and representation).
  • h - It specifies hex string, low nibble first.
  • H - It specifies hex string, high nibble first.
  • i - signed integer (machine dependent size and byte order).
  • I - It specifies unsigned integer (machine dependent size and byte order).
  • l - It specifies signed long (always 32 bit, machine byte order).
  • L - It specifies unsigned long (always 32 bit, machine byte order).
  • n - It specifies unsigned short (always 16 bit, big endian byte order).
  • N - It specifies unsigned long (always 32 bit, big endian byte order).
  • s - It specifies signed short (always 16 bit, machine byte order).
  • S - It specifies unsigned short (always 16 bit, machine byte order).
  • v - It specifies unsigned short (always 16 bit, little endian byte order).
  • V - It specifies unsigned long (always 32 bit, little endian byte order).
  • x - It specifies NUL byte.
  • X - It specifies Back up one byte.
  • @ - It specifies NUL-fill to absolute position.
data It specifies the binary data to be unpacked.

Example

An example of the
function is:

<?php

echo "<pre>";

$content= "PHP";

print_r( unpack("C*",$content));

?>

Output


unpack-function-in-php.gif

PHP usleep() Function

The PHP Miscellaneous usleep function delays execution in microseconds and this function returns no value.

Syntax

unsleep(microseconds)

Parameter in usleep function

The parameter of the function is:

Parameter Description
microseconds It specifies the number of micro seconds to delay the script.

Example

An example of the
function is:

<?php

print_r(date('h:i:s'));

usleep(10000000);

echo "</br>";

print_r(date('h:i:s')); // it prints after 10 seconds time.

?>

Output

u-sleep-function-in-php.gif

COMMENT USING

Trending up