PHP Function: Built-in Functions part 8

Misc. Functions 

The die() and exit functions provide useful control over the execution of your script, offereing an ‘escape route’ for programming errors.

Outputs a given message and terminates the script when a returned value is false. Its syntax is


for example, you would use the following code to print a message and stop the execution of your script upon failure to connect to your database:

$connection = mysql_connect(“servername”, “username”, “password”)
or die(“Can’t connect to database.”);

The exit statement terminates the execution of the current script at the point where the exit statement is made.

sleep() and usleep() 
The sleep() and usleep() functions put a pause, or delay, at a given point in the execution of your PHP code. The syntax of these functions is


The only defference between sleep() and usleep() is that the given wait period for sleep() is in seconds, and the wait period for usleep() is in microseconds.

The uniqid() function generates a unique identifier, with a prefix if you want one.
Its syntax is


That’s boring though. Suppose you want a unique ID with a prefix of “phpuser”
You would use

$id = uniqid(“phpuser”)
echo $id;

and you would get something like phpuser38b320a6b5482.

But if you use something cool like

$id = md5(uniqid(rand()));
echo $id;

you would get an ID like 999d8971461bedfc7caadcab33e65866

Next php function: Built-in Functions part 9