H I O X INDIA
FREE PHP Topics
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Directories 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)   Bookmarks :-)  
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

PHP Topics
Introduction
Common Header
Random Number
Type
Form Variables
Date and Time
PHP Mysql Functions
File Handling
Error Handling
DB Size
PHP Mail
String Tokens
Running PHP from JS
Array To JS
Array to PHP
Encryption
Ask Your Doubts
Feedback





Error Handling in PHP


Topic

I want to stop or block error, warning messages for only one operation?
or
How to restrict error message for specific functions?


Explanation

Some times we will have to block warning message for certain operations.
It can be done very easily.

Stop error reporting for specific operation in PHP:
    To stop error or warning messages on web pages for certain method or operation you can use the character "@"

For example, here we try to open a file that does not exit, and call a include with a wrong file name, we will get warning messages as below
Code Used: e.g:
<?php
echo("test for error message 1");
fopen('test.txt',r);
echo("test for error message 2");
include "test.php";
?>

Result: test for error message 1
Warning: fopen(test.txt) [function.fopen]: failed to open stream: No such file or directory in /home/myscript/public_html/tutorials/php/error-handling/restrictErrors.php on line 74


test for error message 2
Warning: include(test.php) [function.include]: failed to open stream: No such file or directory in /home/myscript/public_html/tutorials/php/error-handling/restrictErrors.php on line 76

Warning: include() [function.include]: Failed opening 'test.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/myscript/public_html/tutorials/php/error-handling/restrictErrors.php on line 76

Now with the same code we will restrict or stop error reporting for one operation say include call. To restrict errors, we have to use the character @ before the function. i.e instead of include.... we will use @include....
Code Used: e.g:
<?php
echo("test for error message 1");
fopen('test.txt',r);
echo("test for error message 2");
@include "test.php";
<?php
?>

Result: test for error message 1
Warning: fopen(test.txt) [function.fopen]: failed to open stream: No such file or directory in /home/myscript/public_html/tutorials/php/error-handling/restrictErrors.php on line 103


test for error message 2

Thus we can prevent the errors and warnings from being shown on the web page.

Next>> different error levels and how to use them




others


privacypolicy     licence     sitemap
(c) copyright, 2004 hioxindia.com