exit
Terminates the program.
Syntax
exit(string|int $status = 0): neverParameters
| Parameter | Type | Description |
|---|---|---|
| $status | string|int | Optional. A message to print, or an exit code. |
Return value
neverThe construct never returns.
Examples
Stopping with a message:
<?php
echo "Step 1\n";
exit('Stopped');
echo "Step 2\n";Output:
Step 1
StoppedAfter a redirect:
<?php
header('Location: /login');
exit;Notes
Always put exit after header('Location: …'): otherwise the rest of the page's code runs. die is the same thing under another name.
See also
- header — Sends an HTTP response header.
- error_log — Writes a message to the error log.
- register_shutdown_function — Registers a function to run when the program ends.
Related recipes
In the tutorial
- Command-line scripts — PHP in the terminal — running a script, argv arguments, reading input, exit codes, getopt and running on a schedule.