die Function in PHP
The die() function in PHP is used to display the error message that specifies and exits from the current script. When an error condition is detected, the die() function can be used to exit the code execution. An error can occur when we try to divide a number with zero or try to open a file that doesn’t exist, open a database that does not exist, etc.
<?php $fileName = "/myfiles/emp.txt"; fopen($fileName, "r") or die("unable to open the file $fileName"); ?>
In the above code, we open the file called ’emp’ using the fopen() function. If the file is already created there is no error occurs. But the file does not appear directly it shows an error. So we can handle that error using the die() function.
If the file ’emp.txt’ does not occur, we will get the output message “Unable to open the file $fileName” on the screen. If there is no file with name /myfiles/emp.txt at the given location. The die() function is similar to the exit() function which is also used to exit code execution.
I hope you have understood a little about Die in-bult Function PHP in this article. If you have any doubts about this then please comment below.
Read also: PHP Characteristcs