Introduction to the die() Function

The die() function in PHP is used to print a message and terminate the current script execution. It is essentially the same as the exit() function, which is used to halt the execution of a script.

Usage of the die() Function

The die() function takes one optional argument, which is the message to be displayed when the script is terminated. If no message is provided, the function will simply terminate the script without displaying any output.

Example Usage of the die() Function

Here's an example of how the die() function can be used in PHP:


$name = "John";

if ($name != "Jane") {
  die("Access denied!");

echo "Welcome, $name!";

In this example, the die() function is used to terminate the script if the value of the $name variable is not equal to "Jane". If the condition is met and the function is executed, the message "Access denied!" will be displayed and the script will terminate without executing the echo statement.


In conclusion, the die() function in PHP is a simple yet powerful tool for terminating the execution of a script and displaying an error message if necessary. It takes an optional message argument and can be used to control the flow of your code based on certain conditions.

Practice Your Knowledge

What is the function of 'die()' in PHP?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?