PHP die() Function

What is die() Function?

If you want to display a message and terminating the script without executing the remaining code in it, use die() function.

Syntax:

die(message_or_number)

Parameters:

message_or_number (optional): This could be a string or a number-

  • For string – If the parameter is a message, the function prints the message and then terminates the program. Check example 1.
  • For number – If the parameter is a number, the function doesn’t print the message but terminates the program. These numbers have no significance when your php script is invoked by web server. Check example 2. These numbers are exit status code that ranges from 0 to 255. These codes are useful for command line programming. You shouldn’t use the exit code 255 as it is a reserved status code.

Return Values:

This function returns no value.

Examples:

Example 1:

<?php
die("The script is terminating...");
echo "This text will not be displayed.";
?>

Output:

The script is terminating…

Explanation:

The die() function prints the “The script is terminating…” and terminates the PHP script. And the next line will not be executed.

Example 2:

<?php
echo "This is the last line before die() function";
die(0);
echo "This text will not be displayed.";
?>

Output:

This is the last line before die() function

Explanation:

The die(0) function terminates the script normally. Number inside the die() function is not displayed in the output.

Example 3:

<?php
header("Location: file.php");
die();
echo "This text will not be displayed.";
?>

Explanation:

The script will redirect the current script to the file.php script. And the die() function further ensures any no further execution of this script terminating the script at that point.

Example 4:

<?php
echo "Example of PHP die() function";
function shutdown(){
    $filename = fopen("D:/xampp/htdocs/php/logfile.txt", "w");
    fwrite($filename, "The script last closed at: " . date("Y-m-d H:i:s"));
    fclose($filename);
}
register_shutdown_function("shutdown");
die;
echo "This text will never be executed.";
?>

Output:

Example of PHP die() function

Explanation:

Before terminating the script by die language construct at line 10, the register_shutdown_function() function executes shutdown() function.

Practical Usages of die() Function:

  • Use die() function after a redirect function. Check example 3.

Notes on die() Function:

  • The die() function can be used as a language construct without parentheses like echo etc. It terminates the script like the die() function. Also you can keep the status code empty. So, all the following examples are same-
    <?php
    die;
    die();
    die(0);
    ?>
    
  • Even though the die() function terminates the PHP script, the script executes any shutdown functions (check example 4) and object destructors mentioned before the die() function.
  • This function is same as exit() function.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP die() Function

die() which is one of the built-in miscellaneous functions in PHP is very helpful to stop program execution at a specific point in your script and terminates it displaying a message if you wish to do so.

Reference:

https://www.php.net/manual/en/function.die.php