Error Handling Function in PHP

Error handling function in PHP. 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. 

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

 

Output:

inlarn

Die 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 version of the exit() function. 

Files in PHP

$_FILES is a two-dimensional associative global array containing things being uploaded using the HTTP POST method, and it contains file attributes such as: Attribute. Description. [name] The name of the file that is being uploaded. 

fopen in PHP and How it Works in Error handling function in PHP

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.

If Else Statement in PHP

If a particular condition is true, the if/else statement executes a block of code. If the condition is not met, another piece of code can be run. The if/else statement is a type of “Conditional” Statement in JavaScript, which is used to conduct different actions based on distinct situations.