PHP Function Reference

PHP die() Function

The PHP die() function outputs a message and terminates execution of the current script.

Shutdown functions and object destructors will always be executed even if die is called.

die is a language construct and it can be called without parentheses if no status is passed.

Note: Please note that, this is a language construct and not a function. This language construct is equivalent to exit().




status Optional. Specify status as int or string.
  • If status is a string, this function prints the status just before exiting.
  • If status is an int, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

Return Value

No value is returned.

Example: die() example

The example below shows how to use die() function.


$filename = 'test.txt';
$file = fopen($filename, 'r')
    or die("Unable to open file $filename");

The output of the above code will be:

Unable to open file test.txt

Example: exit status example

In the example below status is passed as an int which is used as the exit status by the program.

//exit program normally

//exit with an error code
die(0376); //octal

Example: shutdown functions and destructors run regardless

The example below shows that the shutdown functions and object destructors are executed even if die is called.

//defining a class
class Foo {
  public function __destruct() {
    echo 'Destruct: ' . __METHOD__ . '()' . PHP_EOL;

//defining a shutdown function
function shutdown() {
  echo 'Shutdown: ' . __FUNCTION__ . '()' . PHP_EOL;

//creating an object of class Foo
$foo = new Foo();

//registering the shutdown function

//calling die
echo 'This will not be displayed.';

The output of the above code will be:

Shutdown: shutdown()
Destruct: Foo::__destruct()

❮ PHP Miscellaneous Reference