Hello Readers,
1. In php both die() and exit() are equivalent functions.
2. Basically die is used to throw an exception while exit is not, it is only used to exit the process.
3. The die() function is used to print the message and exit the script or it may be used to print alternate message.
See the below example of die function:
Example of die:
<?php
die("this is Php die example");
//this will print the message
$x="hello test";
die($x)
//this will show blank screen.
?>
In the above example there is no output because we pass the $variable inside the die function which not show anything in php.
Another example of die:
<p> Example of die():</p>
<?php
$site = "http://www.w3schools.com/";
fopen($site,"r")
or die("Unable to connect ");
?>
In this example if file not open or not connect it show the message unable to connect means the string passes the die is displayed.
4. The difference between die() and exit() in PHP is their origin.
exit() is from exit() in C.
die() is from die in Perl.
5. In php exit() terminates the script without displaying any message but die() also terminates the script with display the message.
6. The syntax of exit() function in php:
<p>syntax of exit()</p>
<p>Syntax
exit(message)</p>
7. The syntax of die() function in php:
<p>syntax of die() </p>
<p>Syntax die(message)</p>
0 Comment(s)