PHP exit

Summary: in this tutorial, you will learn how to use the PHP exit construct to terminate a script immediately.

Introduction to the PHP exit construct

The exit construct ouputs a message and immediately terminates the execution of the current script.

Here’s the syntax of the exit construct:

exit ( string $status = ? ) : void
Code language: PHP (php)

If you use the $status as a string, the exit will output the $status before terminating the script.

However, if you use the $status as an integer:

exit ( int $status ) : void
Code language: PHP (php)

The exit will use the $status as the exit status and won’t output the value. The valid range for the $status is from 0 to 255. PHP reserves the status 255. And the status 0 indicates that the script is terminated successfully.

Note that exit is not a function, but a language construct like if...else, foreach, etc. Therefore, you cannot call it using a variable function.

The exit is functionally equivalent to die.

PHP exit examples

Let’s take some examples of using the exit construct.

1) Simple PHP exit example

The following example uses the exit to terminate the script if the file readme.txt doesn’t exist:

<?php $filename = 'readme.txt'; $f = fopen($filename, 'r'); if (!$f) { exit('File ' . $filename . 'does not exist.'); } // process the fie // ... // close the file fclose($f);
Code language: HTML, XML (xml)

2) Using exit in try…catch…finally statement example

The following example uses the exit construct in the try clause. In this case, the exit doesn’t terminate the script immediately. Instead, the catch block executes to handle the exception and then the finally block executes:

<?php try { echo 'Execute the try block...', PHP_EOL; throw new Exception('An error occurs'); exit(1); } catch (\Throwable $e) { echo $e->getMessage() , PHP_EOL; } finally { echo 'Execute the finally block'; }
Code language: HTML, XML (xml)

Output:

Execute the try block An error occurs Execute the finally block
Code language: JavaScript (javascript)

3) Using PHP exit in a function example

The following example defines a function that has an exit construct:

<?php function shutdown() { echo 'Shutting down...'; exit; } shutdown(); // doesn't run echo 'After shutting down...';
Code language: PHP (php)

In this example, the last statement doesn’t execute because the exit terminates the script when the shutdown() function completed.

4) Using PHP exit with header() function

The header() function sends a raw HTTP header. Typically, you use it to redirect the brower to a new URL:

header('Location: https://ww.phptutorial.net');
Code language: JavaScript (javascript)

The header() function must be called before any actual output is sent. In other words, after calling the header() function, the script must not send any output. Therefore, the exit is usually used with the header() function like this:

<?php header("Location: https://www.phptutorial.net/"); exit; ?>
Code language: HTML, XML (xml)

Summary

  • The exit construct outputs a message and terminates the script immediately.
  • The exit won’t terminate the script immediately if it is used in a try block. The catch block will execute if an exception occurs, and the finally block will always execute.
  • Use exit with header() to prevent PHP from sending output when the header was already sent.
Did you find this tutorial useful?