error_reporting

(PHP 4, PHP 5, PHP 7, PHP 8)

error_reportingFixe le niveau de rapport d'erreurs PHP

Description

function error_reporting(?int $error_level = null): int

error_reporting() modifie la directive error_reporting pendant l'exécution du script. PHP possède plusieurs niveaux d'erreurs, utiliser cette fonction configure ce niveau pendant la durée (d'exécution) du script. Si le paramètre optionnel error_level n'est pas défini, error_reporting() retournera uniquement le niveau de rapport d'erreurs courant.

Liste de paramètres

error_level

Le nouveau niveau error_reporting. Il peut être un champ de bits ou une combinaison de constantes. L'utilisation des constantes est vivement recommandée pour assurer une compatibilité maximale avec les futures versions. Au fur et à mesure que de nouveaux niveaux d'erreurs sont créés, les valeurs évoluent, c'est pourquoi les anciennes valeurs n'ont plus forcément la même signification.

Les constantes représentant les niveaux d'erreurs disponibles et la signification de ces niveaux d'erreurs est décrite dans le manuel sur les constantes prédéfinies.

Valeurs de retour

Retourne le niveau d'error_reporting, avant qu'il ne soit changé en error_level.

Note:

L'opérateur de contrôle d'erreur @ modifie le error_level pendant la gestion des erreurs.

Historique

Version Description
8.0.0 error_level est désormais nullable.

Exemples

Exemple #1 Exemple avec error_reporting()

<?php

// Désactiver le rapport d'erreurs
error_reporting(0);

// Rapporte les erreurs d'exécution de script
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Rapporter les E_NOTICE peut vous aider à améliorer vos scripts
// (variables non initialisées, variables mal orthographiées ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Rapporte toutes les erreurs à part les E_NOTICE
// C'est la configuration par défaut de php.ini
error_reporting(E_ALL & ~E_NOTICE);

// Rapporte toutes les erreurs PHP
error_reporting(E_ALL);

// Rapporte toutes les erreurs PHP
error_reporting(-1);

// Même chose que error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

Notes

Astuce

En passant la valeur -1, toutes les erreurs possibles seront affichées, même lors de l'ajout d'autres niveaux et constantes dans les futures versions de PHP. Ce comportement est équivalent à fournir la constante E_ALL.

Voir aussi

add a note

User Contributed Notes 27 notes

up
378
info at hephoz dot de
18 years ago
If you just see a blank page instead of an error reporting and you have no server access so you can't edit php configuration files like php.ini try this:

- create a new file in which you include the faulty script:

<?php
 error_reporting(E_ALL);
 ini_set("display_errors", 1);
 include("file_with_errors.php");
?>

- execute this file instead of the faulty script file 

now errors of your faulty script should be reported.
this works fine with me. hope it solves your problem as well!
up
48
dave at davidhbrown dot us
20 years ago
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.
up
6
jcastromail at yahoo dot es
5 years ago
Under PHP 8.0, error_reporting() does not return 0 when then the code uses a @ character.  

For example

<?php

$a=$array[20]; // error_reporting() returns 0 in php <8 and 4437 in PHP>=8

?>
up
5
ecervetti at orupaca dot fr
17 years ago
It could save two minutes to someone:
E_ALL & ~E_NOTICE  integer value is 6135
up
3
vdephily at bluemetrix dot com
21 years ago
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
?>
up
1
chris at ocproducts dot com
9 years ago
The error_reporting() function will return 0 if error suppression is currently active somewhere in the call tree (via the @ operator).
up
0
&IT
6 years ago
error_reporting(E_ALL);
if (!ini_get('display_errors')) {
    ini_set('display_errors', '1');
}
up
0
lhenry at lhenry dot com
6 years ago
In php7,  what was generally a notice or a deprecated is now a warning : the same level of a mysql error …  unacceptable for me.

I do have dozen of old projects and I surely d'ont want to define every variable which I eventually wrote 20y ago.

So two option: let php7 degrade my expensive SSDs writing Gb/hours or implement smthing like server level monitoring ( with auto_[pre-ap]pend_file in php.ini) and turn off E_WARNING

Custom overriding the level of php errors should be super handy and flexible …
up
2
keithm at aoeex dot com
16 years ago
Some E_STRICT errors seem to be thrown during the page's compilation process.  This means they cannot be disabled by dynamically altering the error level at run time within that page.

The work-around for this was to rename the file and replace the original with a error_reporting() call and then a require() call.

Ex, rename index.php to index.inc.php, then re-create index.php as:

<?php
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));
require('index.inc.php');
?>

That allows you to alter the error reporting prior to the file being compiled.

I discovered this recently when I was given code from another development firm that triggered several E_STRICT errors and I wanted to disable E_STRICT on a per-page basis.
up
0
kevinson112 at yahoo dot com
8 years ago
I had the problem that if there was an error, php would just give me a blank page.  Any error at all forced a blank page instead of any output whatsoever, even though I made sure that I had error_reporting set to E_ALL, display_errors turned on, etc etc.  But simply running the file in a different directory allowed it to show errors!

Turns out that the error_log file in the one directory was full (2.0 Gb).  I erased the file and now errors are displayed normally.  It might also help to turn error logging off.

https://techysupport.co/norton-tech-support/
up
1
Fernando Piancastelli
21 years ago
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.
up
1
j dot schriver at vindiou dot com
25 years ago
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]
up
-1
huhiko334 at yandex dot ru
7 years ago
If you get a weird mysql warnings like "Warning: mysql_query() : 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
http://tinymy.link/mctct
up
-2
teynon1 at gmail dot com
14 years ago
It might be a good idea to include E_COMPILE_ERROR in error_reporting. 

If you have a customer error handler that does not output warnings, you may get a white screen of death if a "require" fails.

Example:
<?php 
  error_reporting(E_ERROR | E_WARNING | E_PARSE);

  function myErrorHandler($errno, $errstr, $errfile, $errline) {
    // Do something other than output message.
    return true;
  }

  $old_error_handler = set_error_handler("myErrorHandler");

  require "this file does not exist";
?>

To prevent this, simply include E_COMPILE_ERROR in the error_reporting.

<?php
  error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
?>
up
-3
Daz Williams (The Northeast)
17 years ago
Only display php errors to the developer...