H I O X INDIA
FREE PHP Topics
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Templates 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)
Español Français 中文 Deutsch Portuguese Japanese தமிழ்
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

PHP Topics
Introduction
Syntax
Data Types
Variables
Operators
Control Structures
Functions
Pre-defined Function
Calendar Functions
Date and Time
Array Functions
Array Functions List1
Array Functions List2
Math Functions
PHP Mysql Functions
File Handling
Error Handling
DB Size
PHP Mail
String Tokens
String Functions
String Functions List1
String Functions List2
Session Functions
Cookies Functions
Form Variables
Running PHP from JS
Array To JS
Array to PHP
Encryption
Common Header
Ask Your Doubts
More about PHP
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 75


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 77

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 77

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 104


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





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