How to Handle PHP Errors

PHP

Error Handling in php is as simple as to cut the cake.
Error Message will show Line Number, File name and Description of the Error comes.
Error Handling is the Important Part while creating Web Scripts.
Error Handling makes your code much more Professional.
Error Handling Using die() Functions

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Error Handling in PHP : Example in PHP</title>
    </head>
    <body>
        <?php
            //Lets Open one Simple Text File
            $f=fopen("myfile.txt","r");

            //If There is no file on a certain Path then it will provide Error
            //"Warning: fopen(myfile.txt) [function.fopen]: failed to open stream:
            //No such file or directory in D:\wamp\www\myfile.php on line 2 "

            // Lets us keep a Validation before opening file

            if(!file_exists("myfile.txt")) {
                die("File Does Not Exist");
            } else {
                $file=fopen("myfile.txt","r");
            }
        ?> 
    </body>
</html>

Custom Error Handler

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Custom Error Handling in PHP : Example in PHP</title>
    </head>
    <body>
        <?php
            error_function(error_level,error_message,
            error_file,error_line,error_context) 
        ?> 

        <?php
            //Error Handler Function
            function mycustomError($errno, $errstr) {
                echo "<b>Error:</b> [$errno] $errstr";
            }

            //set error handler
            set_error_handler("mycustomError");

            //trigger error
            echo($test);
        ?> 
        <?php
            //error handler function
            function MyNewcustomError($errno, $errstr) {
                echo "<b>Error:</b> [$errno] $errstr<br>";
                echo "Webmaster has been notified";
                error_log("Error: [$errno] $errstr",1,
                "abc@pixeltoweb.com","From: cbd@pixeltoweb.com");
            }

            //set error handler
            set_error_handler("MyNewcustomError",E_USER_WARNING);

            //trigger error
            $test=2;
            if ($test>1) {
                trigger_error("Value must be 1 or below",E_USER_WARNING);
            }
        ?> 
    </body>
</html>
-->