exit
exit
Terminate the current script with a status code or message
never **exit** $status
Terminates execution of the script.
and
will always be executed even if is called.
However, finally blocks are never executed.
Shutdown functionsobject destructorsexit
An exit code of is used to indicate that the program
succeeded in its tasks.
Any other value indicates some sort of error occurred during execution.
0
is a special function,
because it has a dedicated token in the parser,
as such it can be used like a statement (i.e. without parentheses) to
terminate the script with the default status code.
exit
Let op: > It is not possible to disable, or create a namespaced function shadowing the global function.
exit
status
If is a string,
this function prints the just before exiting.
The exit code returned by PHP is .
status``status``0
If is an ,
the exit code returned by PHP will be .
`status``int``status`> **Opmerking:** > Exit codes should be in the range to ,
the exit code is reserved by PHP and should not be used. `0``254``255`
Waarschuwing: > Prior to PHP 8.4.0, did not follow PHP's standard , nor respect the
declare. `exit`type juggling semanticsstrict_types Any value not of type was cast to including and values. As of PHP 8.4.0, it follows the usual type juggling semantics and throws a on invalid values. `int``string``resource``array`
As this terminates the PHP script, no value is returned.
Voorbeeld: Basic example
<?php
// exit program normally
exit();
exit(0);
// exit with an error code
exit(1);
?>
**Voorbeeld: example with a **
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
?>
Voorbeeld: Shutdown functions and destructors run regardless
<?php
class Foo
{
public function __destruct()
{
echo 'Destruct: ' . __METHOD__ . '()' . PHP_EOL;
}
}
function shutdown()
{
echo 'Shutdown: ' . __FUNCTION__ . '()' . PHP_EOL;
}
$foo = new Foo();
register_shutdown_function('shutdown');
exit();
echo 'This will not be output.';
?>
Shutdown: shutdown()
Destruct: Foo::__destruct()
Voorbeeld: as a statement
<?php
// exit program normally with exit code 0
exit;
?>
Waarschuwing: > Prior to PHP 8.4.0, was a language construct and not a function, therefore it was not possible to call it using , or .
exitvariable functionsnamed arguments
register_shutdown_functionShutdown functionsobject destructors