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
die(message);
Example
Simple script termination with a message:
<?php
die('Script stopped');
echo 'This will not be executed';
?>
Execution result:
'Script stopped'
Example
Using die for condition checking:
<?php
$res = false;
if (!$res) {
die('Operation failed');
}
echo 'Success';
?>
Execution result:
'Operation failed'
Example
Using die
without a message:
<?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