The Exit Construct
The exit
construct (or its alias die
) immediately halts the execution of a PHP script.
It can accept an optional parameter - a message or a numeric status that will be output before termination.
Syntax
exit;
// or
exit(message);
Example
Simple script termination:
<?php
echo 'This will be executed';
exit;
echo 'This will NOT be executed';
?>
Code execution result:
'This will be executed'
Example
Termination with a message:
<?php
exit('Script terminated');
?>
Code execution result:
'Script terminated'
Example
Usage in a conditional structure:
<?php
$res = [1, 2, 3];
if (empty($res)) {
exit('Array is empty');
}
print_r($res);
?>
Code execution result:
[1, 2, 3]
See Also
-
the
trigger_error
function,
which generates a user-defined error message