Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: error_reporting - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

restore_error_handler" width="11" height="7"/> <error_log
Last updated: Sun, 25 Nov 2007

view this page in

error_reporting

(PHP 4, PHP 5)

error_reporting — 出力する PHP エラーの種類を設定する

説明

int error_reporting ([ int $level ] )

error_reporting() 関数は、 error_reporting ディレクティブを 実行時に設定します。PHP には多くのエラーレベルがあり、 この関数によりスクリプトの持続時間(実行時間)のレベルが設定されます。

パラメータ

level

新しい error_reporting レベル。ビットマスクまたは名前つき定数のどちらかです。将来の バージョンとの互換性を保証するために、名前つき定数の使用が 強く推奨されています。エラーレベルが追加されると、整数の幅は増加します。 そのため、以前の整数を使用するエラーレベルは常に期待通りに動作するとは 限りません。

利用可能なエラーレベル定数の一覧を以下に示します。 これらのエラーの実際の意味は、 定義済みの定数に 記述されています。
error_reporting() レベル定数とビット値
定数
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
6143 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR

返り値

変更前の error_reporting レベルを返します。

変更履歴

バージョン 説明
5.0.0 E_STRICT が追加されました (これは E_ALL には含まれません)。
5.2.0 E_RECOVERABLE_ERROR が追加されました。
6 E_STRICTE_ALL に含まれるようになりました。

Example#1 error_reporting() の例

<?php

// 全てのエラー出力をオフにする
error_reporting(0);

// 単純な実行時エラーを表示する
error_reporting(E_ERROR E_WARNING E_PARSE);

// E_NOTICE を表示させるのもおすすめ(初期化されていない
// 変数、変数名のスペルミスなど…)
error_reporting(E_ERROR E_WARNING E_PARSE E_NOTICE);

// E_NOTICE 以外の全てのエラーを表示する
// これは php.ini で設定されているデフォルト値
error_reporting(E_ALL E_NOTICE);

// 全ての PHP エラーを表示する(ビット 63 は PHP 3 で利用される)
error_reporting(E_ALL);

// error_reporting(E_ALL); と同じ
ini_set('error_reporting'E_ALL);

?>

注意

警告

ほとんどの E_STRICT レベルのエラーは スクリプトのコンパイル時に発生します。そのため、 error_reportingE_STRICT を含むように設定されている環境では これらのエラーを検出できません (逆も同様です)。

参考



restore_error_handler" width="11" height="7"/> <error_log
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
error_reporting
Ole dot Oldhoj at kanoon dot dk
20-Nov-2007 07:05
Well my default debug file is

<?php

/**
 * @author Ole.Oldhoj@kanoon.dk
 * @copyright 2007
 */

$serverIs = explode(' ', php_uname());

// test for - are we on prod or on test
if ($serverIs[1] == "http://kanoon.dk/")
{
 
define("DEBUG", false);
 
error_reporting(0);
 
define("WebServiceServer", "http://kanoon.dk/webservice.amx");
}
else
{
 
define("DEBUG", true);
 
error_reporting(E_ALL);
 
define("WebServiceServer", "http://kanoon.dk/testWebservice.amx/");
}

// Funktion: loggin / emailing
function errorLog($title, $fejl, $subject)
{
  if (
DEBUG)
  {
   
mail("Ole.Oldhoj@kanoon.dk", $title, $subject);
  }

}

?>
bbaez at biospectra dot com
10-May-2007 09:02
frederick noted this in 2005 but want to stress the point here

If you set error_reporting in httpd.conf or within a script (some PHP versions) then you must use the integer value and not the string:

Example httpd.conf:
E_ALL ^ E_NOTICE would be:
php_value error_reporting 6135

otherwise the error will not display during output.
antickon AT gmail.com
04-Apr-2007 09:21
regarding what vdephily at bluemetrix dot com said ( see http://be.php.net/manual/en/function.error-reporting.php#50228 )

<?php
echo $foobar->field;
?>

also initializes $foobar (as an instance of stdClass), so this code will not cause any notices.
bitagenda at gmail dot com
09-Mar-2007 01:36
A simple and effective way to catch Fatal errors.
From here you can go forward with your own ideas and elaborate a detailed error report.

The principle is simple and ready to test on your system

Fatal errors are not currently catched and you should have display errors off in production sites, but may be you need to test the system so you want a fast an easy way to see fatal errors in your currently running site, without the users seeing those Fatal errors as recomended.

You must have this function

function catchFatalErrors($p_OnOff='On'){
    ini_set('display_errors','On');
    $phperror='><div id="phperror" style="display:none">';
    ini_set('error_prepend_string',$phperror);
    $phperror='</div>><form name="catcher" action="/sos.php" method="post" ><input type="hidden" name="fatal"  value=""></form> <script> document.catcher.fatal.value = document.getElementById("phperror").innerHTML; document.catcher.submit();</script>';
    ini_set('error_append_string',$phperror);
}

Now activate your fatal error catcher

catchFatalErrors();

Just to test write this inexistent function

Bitagenda();

That's all. As you can see there will be a form posted with action="/sos.php", of course you can name this page at your liking, in this case is at the site's root.

Have ready your page sos.php, and elaborate on that. Of course change the mail address to yours,and display a message to the user at your liking.

<?php
if (isset($_POST['fatal'])){
 
error_log($_POST['fatal'],1,'bitagenda@gmail.com');
}
?>

 "System Out of Service. Thanks for waiting."

The other fact about this handler is that even if you do not redirect the Fatal Error, prepending
><div style="display:none">
and appending
</div>

Then writting yor personal message

" Sorry, fatal bug."

Will hide the fatal error message from the user, but will be visible in the browsers soure code view.

What happens if you redirect the page before displaying the error? I don´t know, have not tested, but at least you could do it to alert you that a fatal error exists and possibly the fatal error is not displayed (leading to security and at the same time catching the fatal error). Then you could activate this error handler with post-redirection to see what's going on.

;o)

Bitagenda
Alex
19-Jan-2007 08:43
error_reporting() may give unexpected results if the @ error suppression directive is used.

<?php
 
@include 'config.php';
 include
'foo.bar';        // non-existent file
?>

config.php
<?php
 error_reporting
(0);
?>

will throw an error level E_WARNING in relation to the non-existent file (depending of course on your configuration settings).  If the suppressor is removed, this works as expected.

Alternatively using ini_set('display_errors', 0) in config.php will achieve the same result.  This is contrary to the note above which says that the two instructions are equivalent.
Peter
29-Oct-2006 04:57
I seem to have found a strange bug if you accidentally use the wrong bitwise numerical value.

I am running php 5.1.4 but I used the 8191 numerical value when setting the reporting level. This level is not available in my php version (it only became available in php 5.2.0).

I had intended to use the numerical value for E_ALL.

When running an application that used pear I received the following error:-

Strict Standards: Assigning the return value of new by reference is deprecated in /usr/share/pear/PEAR.php on line 563

Strict Standards: Assigning the return value of new by reference is deprecated in /usr/share/pear/PEAR.php on line 566

This error message ONLY displayed the first time the script was invoked. To get the error message back again a server restart had to be carried out. And again after the restart it would only show the first time.

In the script when I changed the numerical value with the constant E_ALL I received no error message at all (this was the only thing that was changed between the 2 tests).

I have no idea if this is something that was caused by an annomoly in pear or wether it is a problem with the way php handles the bitwise for error reporting.
silvan at NOSPAM dot example dot com
04-Oct-2006 10:38
On a shared debugging and production server it is convenient to use
<?php error_reporting(E_ALL); ?>
for debugging.

This will not help in case of parsing errors, so make sure you enable at least E_PARSE in your php.ini. Parse errors should not exist in production scripts.

Still, sometimes your script will not get executed even though no parse error is displayed (just a blank page/ no output at all). As far as I know this only happens when you redeclare a user function or class.

eg.
<?php
error_reporting
(E_ALL);

function
a(){}
function
a(){}
?>

This prevents your script from running like a parse error, but is in fact a fatal run-time error (E_ERROR). Other fatal run-time errors will allow your script to apply the error_reporting, when it is executed before the
error occurs (eg. put error_reporting on the first line of code.)
enalung at gmail dot com
27-Jul-2006 03:58
To the_bug_the_bug at hotmail dot com:

Using integer values like that is not smart. They are subject to change. The constants on the other end will not change regardless of any changes on the integer values.
Chris
10-May-2006 05:32
I found some simple mistakes in the functions I posted yesterday, so here are the corrected versions.
And a good advice: never code in the middle of the night ;)

<?php
function error2string($value)
{
   
$level_names = array(
       
E_ERROR => 'E_ERROR', E_WARNING => 'E_WARNING',
       
E_PARSE => 'E_PARSE', E_NOTICE => 'E_NOTICE',
       
E_CORE_ERROR => 'E_CORE_ERROR', E_CORE_WARNING => 'E_CORE_WARNING',
       
E_COMPILE_ERROR => 'E_COMPILE_ERROR', E_COMPILE_WARNING => 'E_COMPILE_WARNING',
       
E_USER_ERROR => 'E_USER_ERROR', E_USER_WARNING => 'E_USER_WARNING',
       
E_USER_NOTICE => 'E_USER_NOTICE' );
    if(
defined('E_STRICT')) $level_names[E_STRICT]='E_STRICT';
   
$levels=array();
    if((
$value&E_ALL)==E_ALL)
    {
       
$levels[]='E_ALL';
       
$value&=~E_ALL;
    }
    foreach(
$level_names as $level=>$name)
        if((
$value&$level)==$level) $levels[]=$name;
    return
implode(' | ',$levels);
}
?>

<?php
function string2error($string)
{
   
$level_names = array( 'E_ERROR', 'E_WARNING', 'E_PARSE', 'E_NOTICE',
       
'E_CORE_ERROR', 'E_CORE_WARNING', 'E_COMPILE_ERROR', 'E_COMPILE_WARNING',
       
'E_USER_ERROR', 'E_USER_WARNING', 'E_USER_NOTICE', 'E_ALL' );
    if(
defined('E_STRICT')) $level_names[]='E_STRICT';
   
$value=0;
   
$levels=explode('|',$string);
    foreach(
$levels as $level)
    {
       
$level=trim($level);
        if(
defined($level)) $value|=(int)constant($level);
    }
    return
$value;
}
?>
the_bug_the_bug at hotmail dot com
24-Apr-2006 10:20
In response to simon at firepages dot com dot au below:

I wrote a shorter more efficient function which will return a string containing the names of the error levels set in the .ini file:

<?php
function error_level_tostring($intval, $separator)
{
   
$errorlevels = array(
       
2047 => 'E_ALL',
       
1024 => 'E_USER_NOTICE',
       
512 => 'E_USER_WARNING',
       
256 => 'E_USER_ERROR',
       
128 => 'E_COMPILE_WARNING',
       
64 => 'E_COMPILE_ERROR',
       
32 => 'E_CORE_WARNING',
       
16 => 'E_CORE_ERROR',
       
8 => 'E_NOTICE',
       
4 => 'E_PARSE',
       
2 => 'E_WARNING',
       
1 => 'E_ERROR');
   
$result = '';
    foreach(
$errorlevels as $number => $name)
    {
        if ((
$intval & $number) == $number) {
           
$result .= ($result != '' ? $separator : '').$name; }
    }
    return
$result;
}
?>

P.S. With a little modification this function can be made to show the string values of any enumeration.
dave at davidhbrown dot us
07-Apr-2006 12:51
The example of E_ALL ^ E_NOTICE is a 'bit' confusing for those of us not wholly conversant with bitwise operators.

If you wish to remove notices from the current level, whatever that unknown level might be, use & ~ instead:

<?php
//....
$errorlevel=error_reporting();
error_reporting($errorlevel & ~E_NOTICE);
//...code that generates notices
error_reporting($errorlevel);
//...
?>

^ is the xor (bit flipping) operator and would actually turn notices *on* if they were previously off (in the error level on its left). It works in the example because E_ALL is guaranteed to have the bit for E_NOTICE set, so when ^ flips that bit, it is in fact turned off. & ~ (and not) will always turn off the bits specified by the right-hand parameter, whether or not they were on or off.
DarkGool
20-Aug-2005 02:30
In phpinfo() error reporting level display like a bit (such as 4095)

Maybe it is a simply method to understand what a level set on your host
if you are not have access to php.ini file

<?
$bit
= ini_get('error_reporting');
while (
$bit > 0) {
    for(
$i = 0, $n = 0; $i <= $bit; $i = 1 * pow(2, $n), $n++) {
       
$end = $i;
    }
   
$res[] = $end;
   
$bit = $bit - $end;
}
?>

In $res you will have all constants of error reporting
$res[]=int(16) // E_CORE_ERROR
$res[]=int(8)    // E_NOTICE
...
fredrik at demomusic dot nu
23-Jul-2005 08:24
Remember that the error_reporting value is an integer, not a string ie "E_ALL & ~E_NOTICE".

This is very useful to remember when setting error_reporting levels in httpd.conf:

Use the table above or:
<?
ini_set
("error_reporting", E_YOUR_ERROR_LEVEL);
echo
ini_get("error_reporting");
?>

To get the appropriate integer for your error-level. Then use:

php_admin_value error_reporting YOUR_INT

in httpd.conf

I want to share this rather straightforward tip as it is rather annoying for new php users trying to understand why things are not working when the error-level is set to (int) "E_ALL" = 0...

Maybe the PHP-developers should make ie error_reporting("E_ALL"); output a E_NOTICE informative message about the mistake?
alex at modem-help dot com
24-Jun-2005 09:22
Centos4 (Apache/2.0.52, php-4.3.9)

Maintaining development + production sites side-by-side is  possible. Obviously, the production site wants all error-reporting display switched off, whilst the development site is the total reverse. Unfortunately, the php.ini file is global.

Using `ini_set("display_errors","1");' is not possible (ref: 12-Feb-2005 12:28 below) as any fatal errors in the script mean that it (and included scripts) never get acted upon (see php.net/manual/en/ref.errorfunc.php#ini.display-errors).

Unfortunately the suggestion from Fernando Piancastelli (ref: 13-Dec-2004 09:23) did not work for me either. The following *did* work, and allows parse-errors to be displayed on the development site:

php.ini:
error_reporting  =  ~E_ALL
display_errors = Off
display_startup_errors = Off

within an Apache Virtual Host in httpd.conf:
php_flag  display_errors         on
php_flag  display_startup_errors on
php_value error_reporting        2047

The above *should* be surrounded with `<IfModule mod_php4.c> ... </IfModule>' but that did not work either.
phpfanat at yandex dot ru
23-Feb-2005 02:03
If you get a weird mysql warnings like "Warning: mysql_query() [http://www.mysql.com/doc]: Your query requires a full tablescan...", don't look for error_reporting settings - it's set in php.ini.
You can turn it off with
ini_set("mysql.trace_mode","Off");
in your script

And, as of my opinion, it should be NOTICE, not WARNING level.
vdephily at bluemetrix dot com
22-Feb-2005 08:40
Note that E_NOTICE will warn you about uninitialized variables, but assigning a key/value pair counts as initialization, and will not trigger any error :
<?php
error_reporting
(E_ALL);

$foo = $bar; //notice : $bar uninitialized

$bar['foo'] = 'hello'; // no notice, although $bar itself has never been initialized (with "$bar = array()" for example)

$bar = array('foobar' => 'barfoo');
$foo = $bar['foobar'] // ok

$foo = $bar['nope'] // notice : no such index
?>
11-Feb-2005 02:55
Under jjuffermans' note, the editors posted the following:

"Instead of using @ to suppress errors if the file does not exist you should do a conditional include:

if (is_file("nosuchfile.php")) {
 include_once("nosuchfile.php");
}"

This is rather obvious, but has an even more obvious problem: is_file doesn't check on the include_path, which one assumes include_once is highly likely to be using.

While there are any number of kludgy workarounds which can be employed to overcome this problem, it's a structural problem in PHP and should be fixed. Preferably @ should only suppress a file not found error, but not any errors inside the included file if it is found, or failing that at the least is_file or file_exists should have the option to look on the include_path for the file.
Fernando Piancastelli
14-Dec-2004 06:23
The error_reporting() function won't be effective if your display_errors directive in php.ini is set to "Off", regardless of level reporting you set. I had to set

display_errors = On
error_reporting = ~E_ALL

to keep no error reporting as default, but be able to change error reporting level in my scripts.
I'm using PHP 4.3.9 and Apache 2.0.
ferozzahid [at] usa [dot] com
08-Sep-2004 08:31
To be enable to switch between error_reporting during development and release phases, one can define say 'php_error_reporting' in the main configuration file (ini like file: no PHP) for the application as:

# config.ini
# PHP error reporting. supported values are given below.
# 0 - Turn off all error reporting
# 1 - Running errors
# 2 - Running errors + notices
# 3 - All errors except notices and warnings
# 4 - All errors except notices
# 5 - All errors

php_error_reporting=4

# config.ini ends

Setting error_reporting in PHP files would be something like the code below, assuming the function getinivar() returns the variable value from the configuration file.

[code]
// setting PHP error reporting
switch(getinivar('php_error_reporting')) {
case 0: error_reporting(0); break;
case 1: error_reporting(E_ERROR | E_WARNING | E_PARSE); break;
case 2: error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); break;
case 3: error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); break;
case 4: error_reporting(E_ALL ^ E_NOTICE); break;
case 5: error_reporting(E_ALL); break;
default:
    error_reporting(E_ALL);
}
[/code]

Feroz Zahid.
jernberg at fairytale dot se
27-Feb-2003 08:27
tip: if you want your error_reporting()-setting to work with your own error handler you could simply check the error number against the current error bitmask.

function myErrorHandler( $errno, $errstr, $errfile, $errline )
{
  $replevel = error_reporting();
  if( ( $errno & $replevel ) != $errno )
  {
    // we shall remain quiet.
    return;
  }
  echo( "error....." );
}
jjuffermans at chello dot com
05-Jan-2003 11:47
[Editor's Note]
Instead of using @ to suppress errors if the file does not exist you should do a conditional include:

if (is_file("nosuchfile.php")) {
 include_once("nosuchfile.php");
}


Note that when you use the @ to suppress the error of an include file that couldn'd be found, like so:

  @include("nosuchfile.php");

It also suppresses all the parse errors generated in "nosuchfile.php" if it does exist.

It took us a long time before we discovered why we weren't  getting any parse errors... This is it.

Personally, I don't like this... Maybe it can be changed in a future php version? :)

Coditor
rbt at zort.ca
10-Feb-2002 01:25
It should be noted that in apache.conf files the defined values (constants) don't work.  For E_ALL logging, one would use:

php_admin_value error_reporting 2047
cgi at harrison dot org
12-Dec-2000 08:59
[Editor's Note: The error suppression operator (@)can be used to suppress errors for a single expression. To use the operator, place it in front of the expression that you wish to suppress error reporting for. Basic use is:

$fp = @ fopen ('non-existant.file', 'r');

 See the url below for details.]

Error Suppression Operator - Info
http://www.php.net/manual/language.operators.errorcontrol.php
j dot schriver at vindiou dot com
03-Oct-2000 03:37
error_reporting() has no effect if you have defined your own error handler with set_error_handler()

[Editor's Note: This is not quite accurate.

E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING error levels will be handled as per the error_reporting settings.

All other levels of errors will be passed to the custom error handler defined by set_error_handler().

Zeev Suraski suggests that a simple way to use the defined levels of error reporting with your custom error handlers is to add the following line to the top of your error handling function:

if (!($type & error_reporting())) return;

 -zak@php.net]
04-Feb-2000 04:31
The E_NOTICE error reporting level reports the use of undefined variables as an error.

For example:
error_level (E_ALL); # Set error reporting to highest level
if ($foo)            # This will generate an error
  print "bar";       # because $foo is not defined

To avoid this behavior, use isset to test if the given
variable has been defined.

For example:
error_level (E_ALL);
if (isset ($foo))
    print "bar";
webmaster at l-i-e dot com
22-May-1999 06:13
[Editor's Note: E_ALL will contain the result of OR'ing all of the applicable error constants together. For PHP 3, this will be the first 4 E_xxx constants.  For PHP 4, this will be all constants. ]

There is also an E_ALL which is the first 4 E_xxx added up for you...

restore_error_handler" width="11" height="7"/> <error_log
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites