Error Handling in PHP

How to stop or block error, supress warning messages in the page?

Explanation

Error handling in php can be done very easily
Stop error reporting completely in PHP:
To completely turn off error or warning messages on web pages you can use the function "error_reporting(0);"
For example, here we try to open a file that does not exit, we will get a warning as below
Code Used: e.g:
<?php
fopen('test.txt',r);
echo("test for error message");
?>

Result: Warning: fopen(test.txt) [function.fopen Previous]: failed to open stream: No such file or directory in /warningMessages.php on line 72 test for error message

Now with the same code we "set error reporting as 0" before calling other methods.
Code Used: e.g:
<?php
error_reporting(0);
fopen('test.txt',r);
echo("test for turning off warning message");
?>

Result: test for turning off warning message

So we have supressed or blocked all warning and error that are shown on web pages.
We can also use ini_set to turn off any error message being displayed. The property to be used is "display_errors". Note that using ini_set will overwrite all other related properties set using functions.
Code Used: e.g:
<?php
ini_set("display_errors",0);
.........
.........
.........
?>
To reset, use value as 1 instead of 0.

PHP Topics


Ask Questions

Ask Question