A PHP Error occurs when something is wrong in the PHP code. The error can be as simple as a missing semicolon, or as complex as calling an incorrect variable.
To efficiently resolve a PHP issue in a script, you must understand what kind of problem is occurring.
The Four Types of PHP errors are:
1. Warning Error
2. Notice Error
3. Parse Error
4. Fatal Error
A warning error in PHP does not stop the script from running. It only warns you that there is a problem, one that is likely to cause bigger issues in the future.
The most common causes of warning errors are:
For instance:
<?php
echo "Warning error"';
include ("external_file.php");
?>
As there is no “external_file”, the output displays a message, notifying it failed to include it. Still, it doesn’t stop executing the script.
Notice errors are minor errors. They are similar to warning errors, as they also don’t stop code execution. Often, the system is uncertain whether it’s an actual error or regular code. Notice errors usually occur if the script needs access to an undefined variable.
Example:
<?php
$a="Defined error";
echo "Notice error";
echo $b;
?>
In the script above, we defined a variable ($a), but called on an undefined variable ($b). PHP executes the script but with a notice error message telling you the variable is not defined.
Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script.
Parse errors are caused by:
For example, the following script would stop execution and signal a parse error:
<?php
echo "Red";
echo "Blue";
echo "Green"
?>
It is unable to execute because of the missing semicolon in the third line.
Fatal errors are ones that crash your program and are classified as critical errors. An undefined function or class in the script is the main reason for this type of error.
There are three (3) types of fatal errors:
For instance, the following script would result in a fatal error:
<?php
function sub()
{
$sub=6-1;
echo "The sub= ".$sub;
}
div();
?>
The output tells you why it is unable to compile, as in the image below:
Distinguishing between the four types of PHP errors can help you quickly identify and solve problems in your script. Make sure to pay attention to ouput messages, as they often report on additional issues or warnings.
Different types of errors are :
- 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_ALL: Catches all errors and warnings