Error Handling in PHP

In PHP error handling very simple. An error message with filename, line number describing the error and sent to the browser.

PHP Error Handling

error handling is an important part when develop a web application. Without error checking your code is unsecure

Some error handling methods are- :

Use die() statements
error triggers
Custom errors
Error reporting

<?php
$file=fopen("yourfile.txt","r");
?>

If the file does not exist you might get an error like this:

Warning: fopen(yourfile.txt) [function.fopen]: failed to open stream:
No such file or directory in C:\yourfolder\file.php on line 2
Getting an error message like the one above, then we test whether the file exist or not before access it:

<?php
if(!file_exists("yourfile.txt")) {
die("File not found");
} else {
$file=fopen("yourfile.txt","r");
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *