Error handling die() function in php

PHP has a built-in function called die(). Its purpose is to print a message and exit the current php script. It’s the PHP equivalent of the exit() function. Error handling is the process of detecting and responding to errors raised by your program. If you don’t handle errors properly, they can have a lot of unintended consequences. Error handling in PHP is very simple.

<?php
if( !file_exists("inlarn.txt") )
{
die("File is not present");
}
else
{
$file = fopen("inlarn.txt", "w");
}
?>

 

Output:

inlarn

What is fopen in php and why do we need fopen

The fopen function opens and associates a file named filename with stream. The fopen function clears the stream’s error and EOF indicators. The fopen() function in PHP is a built-in function for opening a file or URL. It’s used to attach a resource to a steam by specifying a filename. The fopen() function is passed the filename and mode to be examined as inputs, and it returns a file pointer resource if a match is discovered and False otherwise. By placing a ‘@’ in front of the function name, the error output can be hidden.