• In a programmer, error is a mistake that may be caused by writing incorrect code or incorrect syntax.
  • An error message is displayed on your browser containing the filename along with a message describing the error, location and the line number in which error has occurred.
  • In PHP there are various types of errors that contains basically four main types of errors, they are
    • Syntax Error or Parse Error
    • Fatal Error
    • Warning Error
    • Notice Error

Syntax Error or Parse Error

  • A syntax error is a mistake in the syntax of source code, which can be done by programmers due to their lack of knowledge or concern.
  • At compile time compiler is used to catch the syntax error.
  • Due to common reasons these errors can occur like missing semicolon, unclosed quotes, or unclosed brackets, extra or missing parentheses and many more.
  • While compiling the program syntax error can be caught by the compiler and gives a syntax error or parse error message.

Sample Code

<?php  
/*------------------syntax error-------------------*/
echo "Venkat: Hie! I'm Venkat. </br>";
echo "Lokesh: I'm Lokesh. How are you?"
echo "Venkat: I'm good! and you?";
echo "Lokesh: I'm also good";
?>

Output

Fatal Error

  • Fatal error is another type of error, which is occurred due to the use of undefined function.
  • The PHP compiler understands the PHP code and also recognizes the undefined function.
  • PHP compiler generates a fatal error, when a function is called without providing its definition.

Sample Code

<?php  
/*------------------fatal error-------------------*/
function add($f1, $f2) {
$sum = $f1 + $f2;
echo "Addition:" . $sum;
}

$f1 = 23;
$f2 = 56;

//call the function that is not defined
//Generate fatal error
catch_fatal_error();
//echo "Fatal Error";
?>

Output

Warning Error

  • When the programmer tries to include a missing file there warning error is generated.
  • The PHP function calls that missing file which does not exist and warning error does not stop/prevent the execution of the program.

Sample Code

<?php  
/*-------------------warning error------------------*/
$cmpny = 'Wikitechy';
echo "Warning Error: ";

//include a file in the code
include ('jtp.php');
?>

Output

Notice Error

  • Notice error is also same as warning error, when program contains something wrong, the notice error occurs.
  • Including a notice error, it allows/continue the execution of the program.
  • Notice error does not prevent the execution of the code and generally it occurs when we try to use or access a variable which is undefined.

Sample Code

<?php  
/*------------------notice error-------------------*/
$telecom = "Airtel";
echo $telecom;
echo $automobile;
?>

Output

Categorized in: