Updated April 1, 2023
Introduction to PHP exit
Whenever there is a need to terminate the current script along with a message in PHP, we make use of an inbuilt function called exit function in PHP, though exit function is used to terminate the current script, it does not interrupt the object destructors and shut down functions from being executed and exit function takes one parameter namely message where message represents the message that is to be displayed during the termination of the current script by the exit function or this message can also be a status number during the termination of the script by the exit function.
Syntax:
exit(message)
where,
The message represents the message that is to be displayed during the termination of the current script by the exit function or this message can also be a status number during the termination of the script by the exit function.
Working of exit Function in PHP
- Whenever there is a need to terminate the current script along with a message in PHP, we make use of an inbuilt function called exit function in PHP.
- Though the exit function is used to terminate the current script, it does not interrupt the object destructors and shut down functions from being executed.
- The exit function takes one parameter namely message.
- The parameter message represents the message that is to be displayed during the termination of the current script by the exit function or this message can also be a status number during the termination of the script by the exit function.
Examples of PHP exit
Lets us discuss a few examples.
Example #1
PHP program to illustrate the working of exit function where we try to connect to a website through PHP program and if unable to connect, exit the script along with an error message by making use of exit function:
Code:
<html>
<body>
<?php
#a variable called website is used to store the URL of the website that is to be connected.
$website = "https://www.google.com/";
#fopen function is used in read mode to read the contents of the website
fopen($website,"r")
#in case if the program is unable to connect to the website and read the contents of the website, the current script is terminated along with an error message using exit function
or exit("Unable to connect to the website: $website ");
?>
</body>
</html>
Output:
In the above program, a variable called website is used to store the URL of the website that is to be connected. Then fopen function is used to open the website in read mode to read the contents of the website. In case the program fails to connect to the website or fails
to read the contents of the website, then an error message is displayed while terminating the script by making use of the exit function which is displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
PHP program to illustrate the working of exit function where we try to connect to a website through PHP program and if unable to connect, exit the script along with an error message by making use of exit function:
Code:
<html>
<body>
<?php
#a variable called website is used to store the URL of the website that is to be connected.
$website = "https://www.facebook.com/";
#fopen function is used in read mode to read the contents of the website
fopen($website,"r")
#in case if the program is unable to connect to the website and read the contents of the website, the current script is terminated along with an error message using exit function
or exit("Unable to connect to the website: $website ");
?>
</body>
</html>
Output:
In the above program, a variable called website is used to store the URL of the website that is to be connected. Then fopen function is used to open the website in read mode to read the contents of the website. In case the program fails to connect to the website or fails to read the contents of the website, then an error message is displayed while terminating the script by making use of the exit function which is displayed as the output on the screen.
The output is shown in the snapshot above.
Example #3
PHP program to illustrate the working of exit function where we try to open a file by specifying the path to the location of the file through PHP program and if unable to open the file, exit the script along with an error message by making use of exit function:
Code:
<html>
<body>
<?php
#a variable called filepath is used to store the path to the location of the file that is to be opened.
$filepath = "C:/Users/admin/Desktop/check";
#fopen function is used in read mode to read the contents of the file present at the location specified
fopen($filepath, "r")
#in case if the program is unable to open the file present at the location specified and read the contents of the file, the current script is terminated along with an error message using exit function
or exit("Unable to open the file present at the location $filepath");
?>
</body>
</html>
Output:
In the above program, a variable called filepath is used to store the path to the location of the file to be opened. Then fopen function is used to open the file in read mode to read the contents of the file from the path that specifies the location of the file. In case the program fails to open the file present at the location specified by the path of the file, the program terminates displaying an error message by making use of the exit function which is displayed as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learnt the concept of exit function in PHP through definition, syntax, working of exit function through programming examples, and their outputs.
Recommended Articles
This is a guide to PHP exit. Here we discuss the introduction, syntax, and working of the exit function in PHP along with examples and code implementation. You may also have a look at the following articles to learn more –