Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

exit

Terminates the program.

Syntax

exit(string|int $status = 0): never

Parameters

ParameterTypeDescription
$statusstring|intOptional. 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
Stopped

After 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

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.

Thanks for reading!

Donate to the site — we work hard for you.