Types of PHP Errors explain in detail

Types of PHP Errors explain in detail
47 Views

          An error means something happened wrong in the program. error is a type of mistake. whenever by mistake we right wrong syntax or forget anything which is important to right in a proper manner.
          We can say an error is a condition of having incorrect knowledge or an error is defined as an unexpected, invalid program state from which it is impossible to recover.

          A “mistake” is an error caused by a fault: the fault being silliness, carelessness.

          An error message comes with filename, line number, and a message describing the error is sent to the browser.

          Whenever any error occurred in PHP, it will show in red color in the browser.

There are mainly 4 types of PHP errors. We can see all of them in detail.

  • Parse Error (Syntax Error)
  • Fatal Error
  • Warning Error
  • Notice Error

Parse Error or Syntax Error:

          The parse error occurs if there is a syntax mistake in the source code or in the script, then output is Parse errors. A parse error stops the execution of the script. The syntax error is caught by the compiler. After fixing the syntax error the compiler compiles the code and executes it. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as follows:

The common reason for syntax errors are:

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

Example

<?php

    echo"Welcome To Coding Script"

?>

Output

            In the above code, We missed the semicolon at the end of line. When that happens there will be a parse or syntax error which stops execution of the script, as in the following line:

syntax error, unexpected end of the file, expecting ',' or ';'

Fatal Errors

           Fatal errors are caused when PHP understands what you’ve written, but it recognizes an undeclared function. This means that function is called without the definition of a function. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.

Example

<?php
function fun1()
{
echo “Welcome TO Coding Script”;
}
fun2();
echo “Fatal Error !!”;
?>

Output

          In the above code we defined a function fun1 but we call another function fun2 i.e. func2 is not defined. So a fatal error will be produced that stops the execution of the script. Like as in the following block.

Fatal error: Uncaught Error: Call to undefined function fun2()

Warning Errors

          Warning errors will not stop the execution of the script. The main reason for warning errors is to include a missing file or using the incorrect number of parameters in a function. This means that the PHP function calls the missing file. It will only show the warning in the browser something goes wrong.

Example

<?php
echo "Warning Error!!";
include ("another_file.php");
?>

Output

          In the above code, we include an another_file.php file, however, the another_file.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error !!. Like in the following block:

Warning Error!! 
Warning: include(another_file•php) [function.include]: failed to open stream: No such file or directory

Notice Errors

          Notice that an error is the same as a warning error i.e. in the notice error execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.

Example

<?php
$a=10;
echo "Notice Error !!";
echo $b;
?>

Output

          In the above code, we defined a variable named $a. But we call another variable i.e. $b, which is not defined. So there will be a notice error produced but the execution of the script does not stop, you will see a message Notice Error !!. Like in the following block:

Notice Error !!

Undefined variable $b

Aniket Khote

Aniket Khote, founder of Coding Script. and also a freelancer. He has knowledge of various programming languages, WordPress, and project building.

Leave a Reply