I was confused as to what the @ symbol actually does, and after a few experiments have concluded the following:
* the error handler that is set gets called regardless of what level the error reporting is set on, or whether the statement is preceeded with @
* it is up to the error handler to impart some meaning on the different error levels. You could make your custom error handler echo all errors, even if error reporting is set to NONE.
* so what does the @ operator do? It temporarily sets the error reporting level to 0 for that line. If that line triggers an error, the error handler will still be called, but it will be called with an error level of 0
Hope this helps someone
Opérateur de contrôle d'erreur
PHP supporte un opérateur de contrôle d'erreur : c'est @. Lorsque cet opérateur est ajouté en préfixe d'une expression PHP, les messages d'erreur qui peuvent être générés par cette expression seront ignorés.
Si l'option track_errors est activée, les messages d'erreurs générés par une expression seront sauvés dans la variable globale $php_errormsg. Cette variable sera écrasée à chaque erreur. Il faut alors la surveiller souvent pour pouvoir l'utiliser.
<?php
/* Erreur intentionnelle (le fichier n'existe pas): */
$mon_fichier = @file ('non_persistent_file') or
die ("Impossible d'ouvrir le fichier : L'erreur est : '$php_errormsg'");
// Cela fonctionne avec n'importe quelle expression, pas seulement les fonctions
$value = @$cache[$key];
// la ligne ci-dessus n'affichera pas d'alerte si la clé $key du tableau n'existe pas
?>
Note: L'opérateur @ ne fonctionne qu'avec les expressions. La règle générale de fonctionnement est la suivante : si vous pouvez prendre la valeur de quelque chose, vous pouvez le préfixer avec @. Par exemple, vous pouvez ajouter @ aux variables, fonctions, à include(), aux constantes, etc. Vous ne pourrez pas le faire avec des éléments de langage tels que les classes, if et foreach, etc.
Voir aussi error_reporting() et la section sur la gestion d'erreurs.
En fait, l'opérateur "@" va aussi désactiver les rapports d'erreurs critiques, qui stoppent l'exécution du script. Entre autres, si vous utilisez "@" pour supprimer les erreurs de certaines fonctions, et que cette fonction n'existe pas, ou qu'elle a été mal orthographiée, vous n'aurez aucune indication.
Opérateur de contrôle d'erreur
12-Aug-2008 11:29
27-May-2008 05:29
NB The @ operator doesn't work when throwing errors as exceptions using the ErrorException class
04-Jan-2007 03:58
If you want to log all the error messages for a php script from a session you can use something like this:
<?php
session_start();
function error($error, $return=FALSE) {
global $php_errormsg;
if(isset($_SESSION['php_errors'])) {
$_SESSION['php_errors'] = array();
}
$_SESSION['php_errors'][] = $error; // Maybe use $php_errormsg
if($return == TRUE) {
$message = "";
foreach($_SESSION['php_errors'] as $php_error) {
$messages .= $php_error."\n";
}
return $messages; // Or you can use use $_SESSION['php_errors']
}
}
?>
Hope this helps someone...
error_reporting()==0 for detecting the @ error suppression assumes that you did not set the error level to 0 in the first place.
However, typically if you want to set your own error handler, you would set the error_reporting to 0. Therefore, an alternative to detect the @ error suppression is required.
13-Oct-2006 09:38
To suppress errors for a new class/object:
<?php
// Tested: PHP 5.1.2 ~ 2006-10-13
// Typical Example
$var = @some_function();
// Class/Object Example
$var = @new some_class();
// Does NOT Work!
//$var = new @some_class(); // syntax error
?>
I found this most useful when connecting to a
database, where i wanted to control the errors
and warnings displayed to the client, while still
using the class style of access.
04-Mar-2005 12:25
If you wish to display some text when an error occurs, echo doesn't work. Use print instead. This is explained on the following link 'What is the difference between echo and print?':
http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
It says "print can be used as part of a more complex expression where echo cannot".
Also, you can add multiple code to the result when an error occurs by separating each line with "and". Here is an example:
<?php
$my_file = @file ('non_existent_file') or print 'File not found.' and $string = ' Honest!' and print $string and $fp = fopen ('error_log.txt', 'wb+') and fwrite($fp, $string) and fclose($fp);
?>
A shame you can't use curly brackets above to enclose multiple lines of code, like you can with an if statement or a loop. It could make for a single long line of code. You could always call a function instead.
27-Dec-2004 12:19
Better use the function trigger_error() (http://de.php.net/manual/en/function.trigger-error.php)
to display defined notices, warnings and errors than check the error level your self. this lets you write messages to logfiles if defined in the php.ini, output
messages in dependency to the error_reporting() level and suppress output using the @-sign.
26-Jul-2003 01:04
With set_error_handler() you bypass the standard error handler, which takes care of @.
if (!($fp = @fopen('not_a_file', 'r')))
trigger_error("Can't open file!", E_USER_WARNING);
... generates ...
Warning: fopen("not_a_file", "r") - No such file or directory in index.php on line 19.
User Warning : Can't open file! in index.php on line 20.
... when I use my own error handler. With the standard error handler I only get the second warning.
If someone knows how to use @ with your own error handler, let me know.
05-Jul-2003 04:59
if you create a new variable by assigning to it the error
suppressed value of an unset variable, the new variable
will be set, with a value of (I believe) null:
$new_variable // previously not set
= @$nonexistent_variable; // also not set
$next_variable = $new_variable // no warning generated
It should be noted that suppressed error reporting is inherited, so to speak.
Consider this function:
function warning() {
$return = 10 / 0;
return $return;
}
This line will produce a warning;
var_dump(warning());
While these will not:
var_dump(@warning());
@var_dump(warning());
This might not be so obvious for some people; I know I didn't expect this behaviour.
To suppress errors for a method inside a class, place the @ operator before the object and not before the method name.
// DO:
@$this->foo($bar);
// DON'T:
$this->@foo($bar);
21-Aug-2002 06:50
When you check if an index of an array is set, you imply that the array itself already exists.
So
if ( isset ( $array [ 'index' ] ) ) {
}
would generate a notice if $array is not defined, but not if $array _is_ defined, but the index 'index' not.
And so on for nested arrays ofcourse
07-Jul-2002 08:31
I don't know if this is a feature or bug, but this doesn't work:
if (!(isset(@$GLOBALS['SPEEDY_GLOBAL_VARS']['PAGE_NAME'])))
On the other hand, this works:
if (!(@isset($GLOBALS['SPEEDY_GLOBAL_VARS']['PAGE_NAME'])))
Regards,
Uri.
