PHP Error Types

PHP Error is some fault or mistake in a coding program. Error may occur due to incorrect syntax or wrong logic. It is a type of mistakes or condition of having incorrect knowledge of the code.

An error message will be displayed on your browser containing the filename along with location, a message describing the error, and the line number in which error has occurred.

There are usually different types of error. There are 4 mainly types of errors occured in PHP as per as below.

  1. Fatal Error
  2. Syntax Error or Parse Error
  3. Warning Error
  4. Notice Error

PHP Fatal Error

Fatal Error is occurred due to the use of undefined function. This means that function is called without the definition of function. PHP compiler generates a fatal error, if function is not found.

PHP Example :

<?php
     function mult($i, $j)
    {
        $num = $i + $j;
        echo "Multiplication is  = " . $num;
    }
    $i = 5;
    $j = 15;
    mult($i, $j);
    div($i, $j);
?>

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function div() in C:\xampp\htdocs\phptraining\error.php:11
Stack trace:
#0 {main}

Explanation : function div() is not found. so it generates PHP Fatal error.
Note : Execution is stopped when PHP Fatal error is generated.

PHP Syntax Error or Parse Error

Syntax Error or Parse Error is occurred due to mistake in the syntax of source code. This is the type of error done by the programmer in the source code of the program.The syntax error is caught by the compiler. After fixing the syntax error the compiler compile the code and execute it. It is also called as Parse error.

PHP Example :

<?php  
        echo "Learn PHP by aryatechno </br>";  
        echo "Learn HTML by aryatechno </br>";
        echo "Learn JAVA by aryatechno </br>"
        echo "Learn CSS by aryatechno </br>";
?> 

Output:

PHP Parse error:  syntax error, unexpected token "echo", expecting "," or ";" in C:\xampp\htdocs\phptraining\error.php on line 5

Explanation : semicolon is missing in line 5 so it gives an error message.
Note : Execution will be stopped when PHP Syntax Error or Parse Error is generated.

 

PHP Warning Error

A warning is generated when the programmer tries to include a missing file using include() or include_once() function. The PHP function calls that missing file which does not exist. The warning error does not stop/prevent the execution of the program.

PHP Example :

<?php  
        echo "Learn PHP by aryatechno </br>";  
        include("learn.php");
        echo "Learn HTML by aryatechno </br>"; // execution of the program will be continue after warning error
    ?> 

Output:

PHP Warning:  include(learn.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\phptraining\error.php on line 3
PHP Warning:  include(): Failed opening 'learn.php' for inclusion (include_path='.:/usr/share/php') in C:\xampp\htdocs\phptraining\error.php on line 3

Explanation : This program call an undefined file learn.php which are not available. So it generates warning error. .
Note : The warning error does not stop/prevent the execution of the program.

PHP Notice Error

It is similar to warning error. When the program contains something wrong like access to undefined variable, the notice error occurs but it allows the execution of script.

PHP Example :

<?php  
        echo "Learn PHP by aryatechno </br>";  
        $num=500;
        
        echo $str;
        echo "Number is $num";
    ?> 

Output:

PHP Notice: Undefined variable: automobile in C:\xampp\htdocs\phptraining\error.php on line 5

Explanation : This program call an undefined variable $str which are not available in program . So it generates PHP Notice.
Note : Notice error does not prevent the execution of the code.

Comments

Leave a Reply

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

39011