Heim >Backend-Entwicklung >PHP-Tutorial >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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Lets us discuss a few examples.
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.
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.
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.
In diesem Artikel haben wir das Konzept der Exit-Funktion in PHP durch Definition, Syntax, Funktionsweise der Exit-Funktion anhand von Programmierbeispielen und deren Ausgaben kennengelernt.
Das obige ist der detaillierte Inhalt vonPHP-Exit. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!