PHP exit(): Stop execution of the current PHP script

The PHP exit() function is used to stop running a PHP script or file that is already running. For example:

<?php
   if(isset($_SESSION['user']))
   {
      // block of code to process user
   }
   else
   {
      header('Location: login.htm');
      exit();
      // some block of code
   }
   // other block of code
?>

Since the session variableĀ user is not set, the program flow goes to the "else" block, and the following two statements get executed:

header('Location: login.htm');
exit();

The header() function redirects the page to the login.htm page, whereas the exit() function skips the execution of any remaining PHP script. That is, the execution of current PHP scripts stops when an exit() function is encountered.

The output of the above PHP example is shown in the snapshot given below:

php exit function

Since theĀ header() function redirects to another page, that is the login.htm page. Therefore, I think I have to create another example of the exit() function that shows how it terminates the execution of a current PHP script:

<?php
   $x = 10;
   for($i=1; $i<$x; $i++)
   {
      if($i==4)
         exit();
      echo "The value of \$i is $i <br>";
   }
   echo "The value of \$i is $i <br>";
   echo "Some other text...";
   // some other block of code...
?>

The output of the above PHP example is shown in the snapshot given below:

php exit function

That is, when the value of $i becomes equal to 4, the exit() function is encountered, which terminates the execution of the current PHP scripts. Therefore, in the above example, including the remaining values of $i, the statements available right after the loop have also not been executed.

PHP exit() Syntax

The syntax of the exit() function in PHP is:

exit(message|status);

That is, either we can display some message before stopping the execution of the current PHP script or we can provide some status code. The status code or number will not be displayed on the output; rather, it is used for exit status.

But let me tell you one thing: when an exit(); statement is encountered, the execution of current PHP scripts halts or stops, but shutdown functions and object destructors will complete its execution.

Note: If you are not passing any status code or message to the exit() function, then you can write simply exit; to exit the program normally. That is:

All three statements do the same thing: exit the program normally.

To print some messages before exiting the program, follow the example given below:

<?php
   $x = 10;
   for($i=1; $i<$x; $i++)
   {
      if($i==4)
         exit("Exiting...");
      echo "The value of \$i is $i <br>";
   }
   echo "The value of \$i is $i <br>";
?>

The output should be:

The value of $i is 1
The value of $i is 2
The value of $i is 3
Exiting...

Advantages of the exit() function in PHP

Disadvantages of the exit() function in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!