PHP | Types of Errors

Error is the fault or mistake in a program. It can be several types. Error can occur due to wrong syntax or wrong logic. It is a type of mistakes or condition of having incorrect knowledge of the code.

There are various types of errors in PHP but it contains basically four main type of errors.

  1. Parse error or Syntax Error: It 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. Parse errors can be caused dues to unclosed quotes, missing or Extra parentheses, Unclosed braces, Missing semicolon etc
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $x = "geeks";
    y = "Computer science";
    echo $x;
    echo $y;
    ?>

    chevron_right

    
    

    Error:

    PHP Parse error:  syntax error, unexpected '=' 
    in /home/18cb2875ac563160a6120819bab084c8.php on line 3
    

    Explanation: In above program, $ sign is missing in line 3 so it gives an error message.

  2. Fatal Error: It is the type of error where PHP compiler understand the PHP code but it recognizes an undeclared function. This means that function is called without the definition of function.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    function add($x, $y)
    {
        $sum = $x + $y;
        echo "sum = " . $sum;
    }
    $x = 0;
    $y = 20;
    add($x, $y);
      
    diff($x, $y);
    ?>

    chevron_right

    
    

    Error:

    PHP Fatal error:  Uncaught Error: 
    Call to undefined function diff() 
    in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php:12
    
    Stack trace:
    #0 {main}
      thrown in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php on line 12
    

    Explanation : In line 12, function is called but the definition of function is not available. So it gives error.

  3. Warning Errors : The main reason of warning errors are including a missing file. This means that the PHP function call the missing file.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php 
      
    $x = "GeeksforGeeks";
      
    include ("gfg.php");
      
    echo $x . "Computer science portal";
    ?>

    chevron_right

    
    

    Error:

    PHP Warning:  include(gfg.php): failed to 
    open stream: No such file or directory in 
    /home/aed0ed3b35fece41022f332aba5c9b45.php on line 5
    PHP Warning:  include(): Failed opening 'gfg.php'
     for inclusion (include_path='.:/usr/share/php') in 
    /home/aed0ed3b35fece41022f332aba5c9b45.php on line 5
    

    Explanation: This program call an undefined file gfg.php which are not available. So it produces error.

  4. Notice Error: It is similar to warning error. It means that the program contains something wrong but it allows the execution of script.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php 
      
    $x = "GeeksforGeeks";
      
    echo $x;
      
    echo $geeks;
    ?>

    chevron_right

    
    

    Error:

    PHP Notice:  Undefined variable: geeks in 
    /home/84c47fe936e1068b69fb834508d59689.php on line 5
    

    Output:

    GeeksforGeeks
    

    Explanation: This program use undeclared variable $geeks so it gives error message.

PHP error constants and their description :

  • E_ERROR : A fatal error that causes script termination
  • E_WARNING : Run-time warning that does not cause script termination
  • E_PARSE : Compile time parse error.
  • E_NOTICE : Run time notice caused due to error in code
  • E_CORE_ERROR : Fatal errors that occur during PHP’s initial startup (installation)
  • E_CORE_WARNING : Warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR : Fatal compile-time errors indication problem with script.
  • E_USER_ERROR : User-generated error message.
  • E_USER_WARNING : User-generated warning message.
  • E_USER_NOTICE : User-generated notice message.
  • E_STRICT : Run-time notices.
  • E_RECOVERABLE_ERROR : Catchable fatal error indicating a dangerous error
  • E_DEPRECATED : Run-time notices.


My Personal Notes arrow_drop_up

Sudipta sahana BTech 2nd year Cse student

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.