Exit¶
exit() and die() terminates the current execution.
exit() is a language construct, and since PHP 8.4, it is also a function too.
After the end of execution, the registered shutdown functions and destructors are executed. finally blocks are not executed.
They do not have access to the output stream php://output anymore, so echo doesn’t work; but it may still write in files or database, if the connection is still open.
exit() uses its argument to return its execution status. It is transmitted to the original caller of the application. When the argument is a string, it is displayed before ending the process.
<?php
$total = 0;
for($i = 0; $i < 10; ++$i) {
$total += $i;
if ($i == 5) {
exit($total);
}
}
?>
See also PHP | exit( ) Function and PHP | die() & sleep() functions.
Related : Language Construct, Shutdown Function, Destructor, Finally, Propagation, Return Value, set_error_handler(), Testable
Added in PHP 8.1