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:
- Syntax Error or Parse Error
- Fatal Error
- Warning Error
- Notice Error
We will discuss all these errors in detail with examples:
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 concern or knowledge. It is also known as Parse error. Compiler is used to catch the syntax error at compile time.
Note: Syntax error stop the execution of the code.
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.
Example 1: Missing semicolon
Output
Parse error: syntax error, unexpected 'echo' [T_ECHO], expecting ',' or ';' in C:\xampp\htdocs\program\fatalerror.php on line 5
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.
Example 2: Missing dollar symbol
Output
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\program\fatalerror.php on line 5
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.
Fatal Error
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 -
Example: Calling undefined function
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
Fatal error: Uncaught Error: Call to undefined function catch_fatal_error[] in C:\xampp\htdocs\program\fatalerror.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\program\fatalerror.php on line 13
Warning Error
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.
Example: Include missing file
Output
Warning Error: Warning: include[jtp.php]: failed to open stream: No such file or directory in C:\xampp\htdocs\program\fatalerror.php on line 7 Warning: include[]: Failed opening 'jtp.php' for inclusion [include_path='C:\xampp\php\PEAR'] in C:\xampp\htdocs\program\fatalerror.php on line 7
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
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-
Example 2: Access undefined variable
Output
Airtel Notice: Undefined variable: automobile in C:\xampp\htdocs\program\fatalerror.php on line 6
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.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Error is the fault or mistake in a program. It can be several types. Error can occur due to wrong syntax or wrong logic. It is a type of mistakes or condition of having incorrect knowledge of the code.
There are various types of errors in PHP but it contains basically four main type of errors.
- Parse error or Syntax Error: It is the type of error done by the programmer in the source code of the program. The syntax error is caught by the compiler. After fixing the syntax error the compiler compile the code and execute it. Parse errors can be caused dues to unclosed quotes, missing or Extra
parentheses, Unclosed braces, Missing semicolon etc
Example:Error:
PHP Parse error: syntax error, unexpected '=' in /home/18cb2875ac563160a6120819bab084c8.php on line 3
Explanation: In above program, $ sign is missing in line 3 so it gives an error message.
- Fatal Error: It is the type
of error where PHP compiler understand the PHP code but it recognizes an undeclared function. This means that function is called without the definition of function.
Example:Error:
PHP Fatal error: Uncaught Error: Call to undefined function diff[] in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php:12 Stack trace: #0 {main} thrown in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php on line 12
Explanation : In line 12, function is called but the definition of function is not available. So it gives error.
- Warning Errors : The main reason of warning errors are including a missing file. This means that the PHP function call the missing file.
Example:Error:
PHP Warning: include[gfg.php]: failed to open stream: No such file or directory in /home/aed0ed3b35fece41022f332aba5c9b45.php on line 5 PHP Warning: include[]: Failed opening 'gfg.php' for inclusion [include_path='.:/usr/share/php'] in /home/aed0ed3b35fece41022f332aba5c9b45.php on line 5
Explanation: This program call an undefined file gfg.php which are not available. So it produces error.
- Notice Error: It is similar to warning error. It means that the program contains something wrong but it allows the execution of
script.
Example:Error:
PHP Notice: Undefined variable: geeks in /home/84c47fe936e1068b69fb834508d59689.php on line 5
Output:
GeeksforGeeks
Explanation: This program use undeclared variable $geeks so it gives error message.
PHP error constants and their description :
- E_ERROR : A fatal error that causes script termination
- E_WARNING : Run-time warning that does not cause script termination
- E_PARSE : Compile time parse error.
- E_NOTICE : Run time notice caused due to error in code
- E_CORE_ERROR : Fatal errors that occur during PHP’s initial startup [installation]
- E_CORE_WARNING : Warnings that occur during PHP’s initial startup
- E_COMPILE_ERROR : Fatal compile-time errors indication problem with script.
- E_USER_ERROR : User-generated error message.
- E_USER_WARNING : User-generated warning message.
- E_USER_NOTICE : User-generated notice message.
- E_STRICT : Run-time notices.
- E_RECOVERABLE_ERROR : Catchable fatal error indicating a dangerous error
- E_DEPRECATED : Run-time notices.