Wednesday, March 26, 2014

Types of errors in PHP

  An error is a type of mistake. We can say an error is a condition of having incorrect or false knowledge or an error is defined as an unexpected, invalid program state from which it is impossible to recover.
  Error can be defined also as "deviation from accuracy or correctness". A "mistake" is an error caused by a fault: the fault being misjudgment, carelessness or forgetfulness. An error with file name, line number and a message that describing the error is sent to the browser.

Types of errors
  Basically there are four types of errors in PHP, which are as follows:
  • Parse Errors (Syntax Error).
  • Fatal Errors.
  • Warning Errors.
  • Notice Errors.
  1. Parse Errors (Syntax Error)
  The parse error occurs if there is a syntax mistake in the script; the output is parse error. A parse error stops the execution of the script. There are many reasons for the occurrence of parse error in PHP. The common reasons for the parse error is the following:
    • Unclosed quotes.
    • Missing or extra parentheses.
    • Unclosed braces.
    • Missing semicolon.
  Example:
<?php
echo "cat";
echo "dog"
echo "lion";
?>
  Output:
  In the above code I missed the semicolon in the second line. When that happens there will be a parse or syntax error which will stop the execution of the script, as in the following image:

  2. Fatal Errors
  Fatal errors occurs when php understand what you have written, however what you'er asking to do cannot be done. Fatal errors stops the execution of the script. If you are trying to access undefined functions, then the output is a fatal error.
  Example:
<?php
function fun1(){
echo "Hello";
}
fun2();
?>
  Output:
    In the above code we defined a function fun1 but we call another function fun2 which is not defined. So a fatal error will be produced that stops the execution of the script, as the following image:

  3. Warning Errors
  Warning errors will not stop the execution of the script. The main reason of warning errors are to include a missing file or pass a wrong number of parameter for a certain function, as the following example.
  Example:
<?php
include("welcome.php");
?>
  Output:

  4. Notice Errors
  Notice errors are same as Warning errors; as the script do not stop, it occurs when you are trying to access an undefined variables, as in this example:
  Example:
<?php
$a = 12;
echo $b;
?>
  Output:
  In the above example we defined a variable $a, and called another "undefined" variable $b, that produced a notice error without stopping the script, a message will be produced like in the following snapshot: