Open In App

PHP | die() & sleep() functions

Improve
Improve
Like Article
Like
Save
Share
Report

die()

The die() is an inbuilt function in PHP. It is used to print message and exit from the current php script. It is equivalent to exit() function in PHP. Syntax :

die($message)

Parameters : This function accepts only one parameter and which is not mandatory to be passed.

  • $message : This parameter represents the message to printed while exiting from script.

Return Value : It has no return value but prints given message while exiting the script. 

Examples :

Input : die("code ends here")
Output : code ends here

Here, die() function ends the 
script with a message "code
ends here "

Applicable versions : This function is applicable in all PHP4 and later versions. 

Program : 

PHP




<?php
// blank url of site
// so that die() is executed
$site = "";
 
// open url else die (exit)
fopen($site, "r")
or die("Unable to connect to given site.");
?>


Output :

Unable to connect to given site.

sleep()

The sleep() is an inbuilt function in PHP. It is used to delay the execution of the program for given amount of seconds. 

Syntax :

int sleep(int $seconds)

Parameters : This function accepts only one parameter and which is mandatory to be passed.

  • $seconds : This parameter represents the delay time in seconds.

Return Value : It returns zero on success, or FALSE on error. This function returns a non-zero value if the call was interrupted by a signal. 

Errors / Exceptions : This function generates E_WARNING when the number of seconds is negative. 

Applicable versions : This function is applicable in PHP 4, PHP 5, PHP 7. 

Program : 

PHP




<?php
// initial timings
echo date('h:i:s') . "\n";
 
// halt for 5 seconds
sleep(5);
 
// timings after halt
echo date('h:i:s');
?>


Output :

01:07:16
01:07:21

Note that, Timing after halt (sleep) is 5 seconds more than timings before halt (sleep). 

References: http://php.net/manual/en/function.die.php http://php.net/manual/en/function.sleep.php

Let us see both the functions in a Tabular form -:

  die() Function sleep() Function
1. The die() function is an alias of the exit() function The sleep() function is used to delay the execution of the script some time.
2. It takes a parameter as a Number It takes a parameter as a Number ( Time in seconds )
3. It does not have any return value The number in Parameter cannot be negative it will also be positive
4. It is supported in PHP version 4+ It returns a value that is True or False
5. The die() function is used to print the message. It is supported in PHP version 4+


Last Updated : 11 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads