×
☰ See All Chapters

PHP Errors

There are four types of errors in PHP.

  • Notice Error 

  • Warning Error 

  • Parse Error 

  • Fatal Error 

Notice Error

Notice error is small information to user like notice of undefined variable usage.

Example:

<?php

$a =20;

echo $x;

echo $y;

?>

In the script above, we defined a variable ($a), but called on an undefined variables ($x, $y).  PHP executes the script but with a notice error message telling you the variable is not defined.

php-errors-0
 

Sometimes you might not see the notice error on browsers, because by default error reporting level is set to E_ALL & ~E_NOTICE means display all error messages except Notice.  By removing ~E_NOTICE notice errors can be displayed by the browser. You can also display notice errors by setting error_reporting(E_ALL) inside your program as below:

<?php

error_reporting(E_ALL);

$a =20;

echo $x;

echo $y;

?>

How to stop PHP from logging PHP Notice errors?

If you're on an Apache server, try setting the value in a .htaccess file. The general format is:

php_flag  log_errors on

php_value error_log  /path/to/error.log

php_value error_reporting integer

Where integer is the value you get from running something like:

echo E_ALL & ~E_NOTICE; // prints 30711

Warning Error

Warning errors are same as Notice errors but warning does not stop script execution. The most common origin of warning errors are:

  1. Access the file that does not exist in a directory. 

  2. Wrong parameters to a function call. 

  3. Trying to access undefined constants. 

<?php

define("TEN", 10);

echo constant("TEN");

echo constant("HUNDRED");

?>

php-errors-1
 

Parse Error

Parse errors are the syntax errors which stops the execution completely. Parse errors are caused by misused or missing symbols in syntax. In the below example we have missed semicolon from line 4 which results into parse error.

php-errors-2
 

Fatal Error

Fatal errors are ones that crash your program and stop the execution of webpage from the line where error occurred.  

Example: Accessing undefined function or class  

There are three types of fatal errors:

  • Startup fatal error – application failed to startup 

  • Compile time fatal error – Using non existing data while programming. 

  • Runtime fatal error – Errors during runtime which cannot be foreseen while programming. 

For instance, In the below program we are trying to access undefined function sayHi() which would result in a fatal error.

<?php

function printHello()

{

    echo "Hello";

}

printHello();

printHi();

?>

php-errors-3
 

 


All Chapters
Author