PHP | Types Of Errors
Php 15-Sep-2021

PHP | Types Of Errors

In this tutorial, you will learn about the types of errors in PHP.

When working in PHP or any PHP framework. Sometimes you forgot to add semicolon on the end of the syntax, mistyped any syntax, defined any variable not correctly, and wrong logic. At that time, errors occur in your script.

Note that, In PHP, Some error prevents the execution of the script. And some errors only display error messages with warning.

There are four types of errors in PHP are:

  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.