The die Construct

The die construct (or its alias exit) immediately stops the execution of a PHP script. You can pass a message that will be output before termination. This construct is often used for error handling or forcing script termination.

Syntax

phpdie(message);

Example

Simple script termination with a message:

php<?php
  die('Script stopped');
  echo 'This will not be executed';
?>

Execution result:

php'Script stopped'

Example

Using die for condition checking:

php<?php
  $res = false;
  if (!$res) {
    die('Operation failed');
  }
  echo 'Success';
?>

Execution result:

php'Operation failed'

Example

Using die without a message:

php<?php
  // Some code
  die();
  // Code below won't execute
?>

See Also

  • the exit function,
    which is an alias of die
  • the trigger_error function,
    which generates a user-level error