PHP die Function | Print a Message and Exit Script

PHP die is a very useful and important inbuilt function in PHP. It prints a message and exits the current running script. Therefore, any code after the die function will not be executed. Also, shutdown functions and object destructors are always executed even after the die function. In this article, we will discuss the PHP die Function. Also, we will discuss a few examples of using it.

Note: This function is an alias of PHP exit Function.

Syntax

die ( $status = 0 );

Parameters

The PHP die function expects only one optional parameter, $status.

  • If $status is a string, the function prints the string before exiting.
  • If $status is an integer, the function will use that value as the exit status code without printing it. Exist Statuses should be in range of 0 to 254. However, PHP reserves 255 for its use and it should not be used. Also, status 0 terminates the program successfully.

Return Value

The PHP die Function does not return any value.

Examples

Let’s consider a few examples of using the die function in PHP.

Example 1: Basic Operation

<?php 
  
	echo 'This is a basic test program';

	echo 'We will check die function';

	die ('Die Executed');

	echo 'This line will not be exexuted';
  
?> 

In the above example, the script will execute only lines 1, 2 and 3. After encountering the die function, the script will terminate and it will not execute any code after that.

OUTPUT:
This is a basic test program
We will check die function
Die Executed

Example 2: Die with Integer Status

Similarly, if you pass an integer value to the function, the function will exit with that status code without printing that value.

<?php 
  
	echo 'This is a test program';

	echo 'We will check die function';

	die (0);

	echo 'This line will not be exexuted';
  
?> 
OUTPUT:
This is a test program
We will check die function

Example 3: Other ways to call

Likewise, you can also call the function without any brackets or passing any parameters.

<?php

    die;  //Correct Way of calling
    die(); //This is also correct

?>

Example 4: Object Destructors

Even after calling the die function, the script will always execute class destructors.

<?php 
  
	class A {

		public function __destruct() {
			echo 'Destructor Called';
		}

	}

	$object = new A();
	die('Die Executed');
  
?> 

In the above example, first, the script will execute die and then it will call the class destructor.

OUTPUT:
Die Executed
Destructor Called

Example 5: Shutdown Functions

Similarly, the script will also include any defined shutdown functions after executing die.

<?php 
  
	function shutdown_handler() {

		echo 'Shutdown Handler Called';

	}

	register_shutdown_function('shutdown_handler');
	die('Die Executed');
  
?> 

In the above example, first, the script will execute die and then call the shutdown handler function.

OUTPUT:
Die Executed
Shutdown Handler Called

Conclusion

In this example, we discussed the PHP die Function. It is very useful in debugging and terminating the script in between. You can read more about it on the Official PHP Documentation. Additionally, you can read about more articles on PHP on Concatly.

Spread the Knowledge
Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *