PHP exit

WBOY
Release: 2024-08-29 13:10:35
Original
352 people have browsed it

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.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

exit(message)
Copy after login

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>
Copy after login

Output:

PHP exit

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>
Copy after login

Output:

PHP exit

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>
Copy after login

Output:

PHP exit

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.

The above is the detailed content of PHP exit. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!