Home >>Advance PHP Tutorial >PHP Error
A common misconception,espcially less experienced developers, is that a "good" proram is one that works without php errors.
In fact, this is not strictly true: a better definition might be that a good program is one that anticipates all possible php error conditions ahead of time and deals with a consisten and correct manner.
Error Type | Description | Example |
---|---|---|
Notices | Non-Critical errors that don't stop PHP from executing a script | Accessing a variable that has not been initialized. |
Warnings | More Serious errors that require attention but still don't stop script execution. | Reading a file that doesn't exist in the stated path. or passed wrong parameters inside a function |
Fatal Errors | Syntax errors, or Critical errors that force PHP to stop executing a script. | Instantiating an object of an undefined class. |
Error Level | Description |
---|---|
E_PARSE | Fatal parse errors |
E_NOTICE | Non-fatal run-time erros(notices) |
E_WARNING | Non-fatal run-time errors(warnings) |
E_ERROR | Fatal run-time errors that force script termination |
E_USER_NOTICE | User-defined non-fatal application errors(notices) |
E_USER_WARNING | User-defined non-fatal application errors(warnings) |
E_USER_ERROR | User-defined fatal application errors |
E_STRICT | Non-fatal run-time errors arising from deprecated PHP syntax | E_ALL | All errors |
error is unwanted error unexpected behavior of a program. an error can be of two types :
Syntax error :if there is any missing symbol or extra symbol the it is said to be error in syntax.
Logical error :if there is an error in logic of a program like divide by zero or object is called but not declared then it is called logical error.
There are 3 types of error
1) notices error
2) warning 3
) fatal error
Notice : In notices error if any variable is not declared but it is used at some point then it will show an notice error. Notice error can hides using "@" before $ Sign.
<?php for($i=1;$i<=100;$i++) { $sum=$sum+$i; } echo "Sum = ".$sum; ?>
Warning : if there is still a mistake in program but output is obtained , this type of error is called warning error.
It is not a critical error because it doesn't terminate the program.
warning error arises due to two conditions:
a) if we are passing wrong type of parameters in parametrized function.
b) if we are calling a external PHP file using include function and file doesn't exist then warning error showed.
<?php include('header.php'); for($i=1;$i<=100;$i++) { $sum=$sum+$i; } echo "Sum = ".$sum; ?>
Fatal error : fatal error is a critical error and it terminates the script immediately.
fatal error comes if we calling external file using require() function and file doesn't exist then showed warning and fatal.
<?php require('header.php'); for($i=1;$i<=100;$i++) { $sum=$sum+$i; } echo "Sum = ".$sum; ?>
Notice : we can handle/hide all the above types of error using error_reporting(1) function.