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:<?php
$x
=
"neveropen"
;
y =
"Computer science"
;
echo
$x
;
echo
$y
;
?>
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:<?php
function
add(
$x
,
$y
)
{
$sum
=
$x
+
$y
;
echo
"sum = "
.
$sum
;
}
$x
= 0;
$y
= 20;
add(
$x
,
$y
);
diff(
$x
,
$y
);
?>
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:<?php
$x
=
"neveropen"
;
include
(
"gfg.php"
);
echo
$x
.
"Computer science portal"
;
?>
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:<?php
$x
=
"neveropen"
;
echo
$x
;
echo
$neveropen
;
?>
Error:
PHP Notice: Undefined variable: neveropen in /home/84c47fe936e1068b69fb834508d59689.php on line 5
Output:
neveropen
Explanation: This program use undeclared variable $neveropen 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.