PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Error Handling in PHP


Tutorials »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





Other Links

web hosting