PHP Exit Function With Examples

The PHP Exit Function is a built-in function which mainly used to print a message and terminate the execution of the current script.

In this article, we will talk about exit function in a detailed explanation as well as example programs that could be helpful to your understanding on this function. This topic is a continuation of the previous topic, entitled PHP in echo.

What is PHP exit function?

In PHP, exit function is one of the important functions in PHP which can only terminate or stop the execution of script. Take note, even though the exit function is called the object destructors and shutdown function it’s still executed.

Further, the message that will be displayed will be as parameters to the exit() function and automatically stop the execution of the script and display a message.

Take note: The die() function is an alias of an exit() their functionalities are similar and work the same.



Parameter values

exit()The exit() function is a language construct that can be called for terminating a program. If you want to terminate a program without a status or a message you can call the exit function without a parenthesis.
messageThe message is a REQUIRED parameter which the purpose is to display a message or a status before the execution of the script will exits.

Once an integer is passed as status parameter, it means that value can be used as an exit to terminate the program and there is no message to be displayed.

The exit status could be in the range of 0 to 254 if it is an integer. Take note: that 255 exit status cannot be used because it was reserved by PHP.


        //declaring variables
        $num1 = 10;
        $num2 = 10.0;
        	//terminating script with a message using exit()
        	exit('The variables are equal');
          //terminating script with a message using exit()
        	exit('The variables are not equal');


The variables are equal


        $website = "";
        // opening a link
        fopen($website, "r")
        //using exit() to display message and terminate script
        or exit("Unable to establish a connection to $website");


Unable to establish a connection to

PHP Exit function more examples

Example – exit() function in try…catch…finally statement


try {
	echo 'Execute the try block...', PHP_EOL;

	throw new Exception('An error occurs');

} catch (\Throwable $e) {
	echo $e->getMessage() , PHP_EOL;
} finally {
	echo 'Execute the finally block';


Execute the try block...
An error occurs
Execute the finally block

Example – exit in a function


function terminated()
	echo 'terminated successfully...';


// doesn't run
echo 'After terminated...';


terminated successfully...

Why is exit() used?

The exit() is used in some instances when the user wants to terminate the program from being used. The function is a void return type that calls all the registered functions at the exit function and terminates the execution of the program.

What is the difference between exit and exit() in PHP?

There is no difference between exit and exit() in PHP they are both the same.

What is exit execution?

The exit execution is a process mainly designed to terminate computer programs from the execution by just calling the exit script function.

Further, an exit function is also better for multithreading programs with just a single call of the exit() function the multithread program will stopped from running.

Does exit terminate all processes?

The exit() function in terms of the parent process could not be terminated as long as there are multiple running child processes.

What does exit 0 do in PHP?

In PHP, a status number 0 is mainly used to terminate a program successfully from being running.

What is the purpose of exit codes?

The purpose of exit code is to terminate a program from being running and to show if the termination of the program is successful. It is also called a return code, or sometimes they call it as an error code. Even though the terminology is slightly different but the functionalities are the same.


In summary, you have learned about PHP exit function. This article also discussed what PHP exit function is, why exit() is used, the difference between exit and exit(), what exit execution is, does exit terminate all processes, what does exit 0 do, and what is the purpose of exit codes.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

1 thought on “PHP Exit Function With Examples”

Leave a Comment