Basically, an error is a mistake in a program that may be caused by writing incorrect syntax or incorrect code. An error message is displayed on your browser containing the filename along with location, a message describing the error, and the line number in which error has occurred.
There are usually different types of error. In PHP, mainly four types of errors are considered:
We will discuss all these errors in detail with examples:
A syntax error is a mistake in the syntax of source code, which can be done by programmers due to their lack of concern or knowledge. It is also known as Parse error. Compiler is used to catch the syntax error at compile time.
These errors can occur due to these common reasons like unclosed quotes, missing semicolon, extra or missing parentheses, or unclosed brackets and many more. While compiling the program, syntax error can be caught by the compiler. It gives a parse error or syntax error message.
<?php /*------------------syntax error-------------------*/ echo "Alex: Hie! I'm Alex. </br>"; echo "Bob: I'm Bob. How are you?" echo "Alex: I'm good! and you?"; echo "Bob: I'm also good"; ?>
Output
Explanation: In this above example, a semicolon (;) was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.
<?php /*------------------syntax error-------------------*/ $telecom = "Airtel"; automobile = "Jaguar"; echo $telecom; echo $automobile; ?>
Output
Explanation: In this above example, dollar ($) symbol was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.
A fatal error is another type of error, which is occurred due to the use of undefined function. The PHP compiler understands the PHP code but also recognizes the undefined function. This means that when a function is called without providing its definition, the PHP compiler generates a fatal error.
A fatal error is generated when a function is called without its definition. See the below example containing the fatal error -
<?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"; ?>
In the above code we have defined the add() function but called other function, which is catch_fatal_error(). Therefore, it generates a fatal error and print an error message on the browser as given below:
Output
A warning is generated when the programmer tries to include a missing file. The PHP function calls that missing file which does not exist. The warning error does not stop/prevent the execution of the program.
The main reason behind generating a warning error is to pass an incorrect number of parameters to a function or to include a missing file.
<?php /*-------------------warning error------------------*/ $cmpny = 'rookienerd'; echo "Warning Error: "; //include a file in the code include ('jtp.php'); ?>
Output
Explanation: In this example, we tried to include a file in our program, which does not exist. So, it generated a warning and displayed an error message.
Notice error is same as warning error. When program contains something wrong, the notice error occurs. But it allows/continue the execution of the program with a notice error. Notice error does not prevent the execution of the code. For example - access to undefined variable.
Generally, notice error occurs when we try to use or access a variable which is undefined. See the below example to understand it-
<?php /*------------------notice error-------------------*/ $telecom = "Airtel"; echo $telecom; echo $automobile; ?>
Output
Explanation: In this above example, we were trying to use a variable $automobile, which was not defined. Therefore, it generated a notice "Undefined variable" and continued the execution of the program.