php errors ( Types of errors in php)
Basically four types of error in php
prase error (syntax error)
Fatal error
Warning error
Notice error
Parse Error
The parse errors occurs if there is a syntax mistake in the
script. The output is parse errors. A parse error stop the
execution of the script. There are many reasons for the
occurrance of parse error in the php.
Unclosed quotes.
Missing or extra parenthess.
unclosed braces.
Missing semicolon.
Fatal Error
Fatal error are caused when php understand what you have
written, however what you are asking it to do can't be done.
Fatal errors stop the execution of the script. If you are trying
to access the undefined functions then the output is fatal error.
An example of a Fatal error would be accessing a property of a non-existent object or require() a non-existent file.
<?php
function add( )
{
echo "Hi php";
}
sum( );
// here fatal error occurs.
?>
Warning Error
Warning errors will not stop the execution of the script. The main reason for warning errors are to include a missing file or using the incorrect number of parameters in a function.
<?php
echo warning error;
include(myfile.php); // myfile.php page not exist in your porject.
?>
Notice Error
Notice that an error is the same as the warning error. In the notice error execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable then produce a notice error.
<?php
$a = "raj";
echo $b;
?>
error_reporting() Function in PHP
<?php
// Turn off error reporting
error_reporting(0);
// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report all errors
error_reporting(E_ALL);
?>
Error Control Operators :- @
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.
If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.
File name : index.php
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
Be aware of using error control operator in statements before include() like this:
<?PHP
(@include("file.php"))
OR die("Could not find file.php!");
?>
This cause, that error reporting level is set to zero also for the included file. So if there are some errors in the included file, they will be not displayed.
To suppress errors for a new class/object:
<?php
// Tested: PHP 5.1.2 ~ 2006-10-13
// Typical Example
$var = @some_function();
// Class/Object Example
$var = @new some_class();
// Does NOT Work!
//$var = new @some_class(); // syntax error
?>
I found this most useful when connecting to a
database, where i wanted to control the errors
and warnings displayed to the client, while still
using the class style of access.
1. If you have set your own default error handler then the error still gets sent to the error handler regardless of the @ sign.
2. As mentioned below the @ suppression only changes the error level for that call. This is not to say that in your error handler you can check the given $errno for a value of 0 as the $errno will still refer to the TYPE(not the error level) of error e.g. E_WARNING or E_ERROR etc
3. The @ only changes the rumtime error reporting level just for that one call to 0. This means inside your custom error handler you can check the current runtime error_reporting level using error_reporting() (note that one must NOT pass any parameter to this function if you want to get the current value) and if its zero then you know that it has been suppressed.
<?php
// Custom error handler
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ( 0 == error_reporting () ) {
// Error reporting is currently turned off or suppressed with @
return;
}
// Do your normal custom error reporting here
}
?>
Previous
Next