Inhaltsverzeichnis
Working of exit Function in PHP
Examples of PHP exit
Example #3
Fazit

PHP-Exit

Aug 29, 2024 pm 01:10 PM
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 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)
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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.

Fazit

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!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

CakePHP-Projektkonfiguration CakePHP-Projektkonfiguration Sep 10, 2024 pm 05:25 PM

CakePHP-Projektkonfiguration

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

CakePHP Datum und Uhrzeit CakePHP Datum und Uhrzeit Sep 10, 2024 pm 05:27 PM

CakePHP Datum und Uhrzeit

CakePHP-Datei hochladen CakePHP-Datei hochladen Sep 10, 2024 pm 05:27 PM

CakePHP-Datei hochladen

CakePHP-Routing CakePHP-Routing Sep 10, 2024 pm 05:25 PM

CakePHP-Routing

Besprechen Sie CakePHP Besprechen Sie CakePHP Sep 10, 2024 pm 05:28 PM

Besprechen Sie CakePHP

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein Dec 20, 2024 am 11:31 AM

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein

CakePHP-Kurzanleitung CakePHP-Kurzanleitung Sep 10, 2024 pm 05:27 PM

CakePHP-Kurzanleitung

See all articles