exit() / die() / sleep() Since: PHP 4(2000)
Functions for terminating script execution or pausing for a specified duration. Commonly used for cleanup after redirects and flow control in batch processing.
Syntax
// Terminates script execution. Optionally outputs a message or sets an exit status. exit($status); // Identical to exit(). Conventionally used to indicate termination due to an error. die($status); // Pauses execution for the specified number of seconds. sleep($seconds); // Pauses execution for the specified number of microseconds. usleep($microseconds);
Function List
| Function | Description |
|---|---|
| exit($status) | Terminates script execution. If a string is passed, it is output before exiting. If an integer is passed, it is used as the exit status code. |
| die($status) | A language construct that behaves exactly like exit(). There is no functional difference; the choice between the two is a matter of expressing intent. |
| sleep($seconds) | Pauses script execution for the specified number of seconds. Returns 0 on success. |
| usleep($microseconds) | Pauses script execution for the specified number of microseconds. There are 1,000,000 microseconds in one second. |
Sample Code
<?php
// Terminate with exit() after a redirect. This is a required pattern after header().
if (!isset($_SESSION['user_id'])) {
header("Location: /login.php");
exit; // Without this, execution continues instead of redirecting.
}
// Output a message and then terminate.
$config = @include('/path/to/config.php');
if ($config === false) {
exit("Failed to load the configuration file.");
}
// die() is commonly used to express termination due to an error.
$conn = @mysqli_connect('localhost', 'user', 'pass', 'db');
if (!$conn) {
die("Could not connect to the database.");
}
// Example of specifying an exit status code.
if ($argc < 2) {
echo "Usage: php script.php [filename]\n";
exit(1); // Status code 1 indicates abnormal termination.
}
// Use sleep() to wait for a fixed amount of time.
echo "Starting process.\n";
echo date('H:i:s') . "\n";
sleep(3); // Pause for 3 seconds.
echo date('H:i:s') . "\n"; // Outputs the time 3 seconds later.
echo "Process complete.\n";
// Use usleep() to wait in sub-second intervals.
for ($i = 0; $i < 3; $i++) {
echo "Retry " . ($i + 1) . "\n";
usleep(500000); // Wait 0.5 seconds.
}
// Example of handling API rate limits with retries.
function call_api_with_retry(string $url, int $max_retry = 3): string|false {
for ($i = 0; $i < $max_retry; $i++) {
$response = @file_get_contents($url);
if ($response !== false) {
return $response;
}
sleep(pow(2, $i)); // Wait 1s, 2s, 4s — exponential backoff.
}
return false;
}
Notes
exit() and die() are language constructs that immediately terminate script execution. They are functionally identical, but by convention exit() is used for normal termination and die() for termination due to an error. Always call exit() immediately after a header("Location: ...") redirect. Without it, PHP continues executing the rest of the script, which can cause security vulnerabilities and unintended behavior.
sleep() is useful in batch processing for rate-limiting and retry delays. Avoid using long sleep() calls during web request handling, as they can cause timeouts.
For controlling error reporting, see error_reporting(). For basic output, see echo / print.
If you find any errors or copyright issues, please contact us.