PHP | exit( ) Function

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script.
The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.
The message to be displayed is passed as a parameter to the exit() function and it terminates the script and displays the message.
The exit() function is an alias of the die() function.

Syntax:

exit(message)

Parameters Used:
The exit() function in PHP accepts one parameter.



  1. message : It is a mandatory parameter which specifies the message or status number to write before exiting the script.
Return Value:
It does not return any value.

Errors And Exceptions

  1. exit() is a language construct and it can be called without parentheses if no status is passed.
  2. If the status passed as a parameter is an integer, that value will be used as the exit status and not be printed.
  3. Exit statuses should be in the range 0 to 254 and the exit status 255 should not be used since it is reserved by PHP.

Below programs illustrate the exit() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Unable to establish a connection to https://www.geeksforgeeks.org

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
//declaring variables
$a=5;
$b=5.0;
  
if($a==$b)
 {
    //terminating script with a message using exit()
    exit('variables are equal');
 }
else
 {
   //terminating script with a message using exit()
    exit('variables are not equal'); 
 }
?>

chevron_right


Output:

variables are equal

Reference :
http://php.net/manual/en/function.exit.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.