PHP Errors: 4 Different Types (Warning, Parse, Fatal, and Notice Error)

Types of errors in PHP; Through this tutorial, i am going to show you types of errors in PHP with it’s syntax, definition and examples.

PHP Errors: 4 Different Types (Warning, Parse, Fatal, and Notice Error)

There are four types of errors in PHP:

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

Notice Error

When trying to access the undefined variable in PHP script at that time notice error occurs. Notice error does not stop the execution of PHP script. It only notice you that there is a problem.

The following example represents the notice error:

<?php
$a="A variable";
echo $a;
echo $b;
?>

In this example, define only $a variable and print it. As well as print $b variable without define. When executing this script, a notice error occurs and display a message, the $b variable is not defined.

Warning Error

The main reason to occurs warning errors is to include a missing file, calling a function with incorrect parameters. It is similar to notice an error, it also does not stop the execution of PHP script. It only reminds you that there is a problem in the script.

The following example represents the warning error:

<?php
echo "Warning error"';
include ("external_file.php");
?>

In this example, include any PHP file that does not exist in your application. At that time, it displays a message that failed to include it.

Fatal Error

When call to undefined functions and classes in PHP script, at that time fatal error occurs. The Fatal error does stop the execution of the PHP script and it display error message.

Note that, if you call the undefined function or class in the script is the main reason to occurs fatal error.

The following example represents the fatal error:

<?php
function addition(){
 $a = 10;
 $b = 50;
 $c = $a + $b;
 echo "Addition of a + b = ".$c;
}
addition();
sub();
?>

In this example, A function is defined, which name is addition(). This function adds 2 variables and prints its result.

When the addition() function is called. After that, sub() function is also called which is not defined. At this time, the fatal error comes and stops the execution of the script with an error message.

Parse Error

The parse error happens, when a syntax mistyped in the script. Parse error does also stops the execution of the PHP script and it display error message.

The following main reason for syntax/parse errors are:

  • Unclosed quotes
  • Missing or Extra parentheses
  • Unclosed braces
  • Missing semicolon

The following example represents the parse error:

<?php
echo "1";
echo "2"
echo "3";
?>

In this example, print a 1, 2, 3. After print “1”, you missed a semicolon. At that time it occurs.

Recommended PHP Tutorials

Leave a Comment