PHP exit() Function

What is exit() Function?

If you want to display a message and terminating the script from executing the remaining code, use exit() function.

Syntax:

exit(message_or_number)

Parameters:

message_or_number (optional): This could be a string or a number-

  • For string – If the parameter is a message, the function prints the message and then terminates the program. Check example 1.
  • For number – If the parameter is a number, the function doesn’t print the message but terminates the program. These numbers have no significance when your php script is invoked by web server. Check example 2.
    These numbers are exit status code that ranges from 0 to 255. These codes are useful for command line programming. You shouldn’t use the exit code 255 as it is a reserved status code.

Return Values:

This function returns no value.

Examples:

Example 1:

<?php
exit("The script is terminating...");
echo "This text will not be displayed.";
?>

Output:

The script is terminating…

Explanation:

The exit() function prints the “The script is terminating…” and terminates the PHP script. And the next line will not be executed.

Example 2:

<?php
echo "This is the last line before exit() function.";
exit(0);
echo "This text will not be displayed.";
?>

Output:

This is the last line before exit() function.

Explanation:

The exit(0) function terminates the script normally.

Example 3:

<?php
echo "Example of PHP exit() function";
function shutdown(){
    $filename = fopen("D:/xampp/htdocs/phpExercise/logfile.txt", "w");
    fwrite($filename, "The script last closed at: " . date("Y-m-d H:i:s"));
    fclose($filename);
}
register_shutdown_function("shutdown");
exit;
echo "This text will never be executed.";
?>

Output:

Example of PHP exit() function.

Explanation:

Before terminates the script by exit language construct, the shutdown function (register_shutdown_function() ) executes shutdown() function.

Example 4:

<?php
header("Location: file.php");
exit();
echo "This text will not be displayed.";
?>

Explanation:

The script will redirect you to the file.php script. And the exit() function further ensures any further execution of this script.

Practical usages of PHP exit() function:

  • You can use exit() function after redirect a script. Check example 4.

Notes on exit() function:

  • The exit() function can be used as a language construct without parentheses like echo etc. It does though terminate the script like the exit() function and you can’t mention any status. All the following examples are same-
    <?php
    exit;
    exit();
    exit(0);
    ?>
    
  • Even though the exit() function terminates the PHP script, the script executes any shutdown functions (check example 3) and object destructors mentioned before the exit() function.
  • This function is same as die() function.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP exit() Function

exit() is one of the many built-in PHP miscellaneous type functions which is used to immediately stop execution of a script.

Reference:

https://www.php.net/manual/en/function.exit.php