PHP Doku:: Fehler-Kontroll-Operatoren - language.operators.errorcontrol.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:

Ein Service von Reinhard Neidl - Webprogrammierung.



Operatoren zur Programmausführung>>


PHP unterstützt einen Operator zur Fehlerkontrolle: Das @-Symbol. Stellt man das @ in PHP vor einen Ausdruck werden alle Fehlermeldungen, die von diesem Ausdruck erzeugt werden könnten, ignoriert.

Ist das track_errors -Feature aktiviert, werden alle Fehlermeldungen, die von diesem Ausdruck erzeugt werden, in der Variablen $php_errormsg gespeichert. Da diese Variable mit jedem neuen Auftreten eines Fehlers überschrieben wird, sollte man sie möglichst bald nach Verwendung des Ausdrucks überprüfen, wenn man mit ihr arbeiten will.

/* Beabsichtigter Dateifehler */
$my_file = @file ('nicht_vorhandene_Datei') or
    die (
"Datei konnte nicht geöffnetwerden: Fehler war:'$php_errormsg'");

// Das funktioniert bei jedem Ausdruck, nicht nur bei Funktionen:
$value = @$cache[$key]; 
// erzeugt keine Notice, falls der Index $key nicht vorhanden ist.


Hinweis: Der @-Operator funktioniert nur bei Ausdrücken. Eine einfache Daumenregel: wenn Sie den Wert von etwas bestimmen können, dann können Sie den @-Operator davor schreiben. Zum Beispiel können Sie ihn vor Variablen, Funktionsaufrufe und vor include() setzen, vor Konstanten und so weiter. Nicht verwenden können Sie diesen Operator vor Funktions- oder Klassendefinitionen oder vor Kontrollstrukturen wie zum Beispiel if und foreach und so weiter.

Siehe auch error_reporting() und den Abschnitt über Error Handling and Logging Functions.


Der @ Fehler-Kontroll-Operator verhindert jedoch keine Meldungen, welche aus Fehlern beim Parsen resultieren.


Zum gegenwärtigen Zeitpunkt deaktiviert der "@" Fehler-Kontrolloperator die Fehlermeldungen selbst bei kritischen Fehlern, die die Ausführung eines Skripts beenden. Unter anderem bedeutet das, wenn Sie "@" einer bestimmten Funktion voranstellen, diese aber nicht zur Verfügung steht oder falsch geschrieben wurde, Ihr PHP-Skript einfach beendet wird, ohne Hinweis auf die Ursache.

12 BenutzerBeiträge:
- Beiträge aktualisieren...
auser at anexample dot com
8.09.2010 13:02
Be aware that using @ is dog-slow, as PHP incurs overhead to suppressing errors in this way. It's a trade-off between speed and convenience.
darren at powerssa dot com
14.07.2010 18:33
After some time investigating as to why I was still getting errors that were supposed to be suppressed with @ I found the following.

1. If you have set your own default error handler then the error still gets sent to the error handler regardless of the @ sign.

2. As mentioned below the @ suppression only changes the error level for that call. This is not to say that in your error handler you can check the given $errno for a value of 0 as the $errno will still refer to the TYPE(not the error level) of error e.g. E_WARNING or E_ERROR etc

3. The @ only changes the rumtime error reporting level just for that one call to 0. This means inside your custom error handler you can check the current runtime error_reporting level using error_reporting() (note that one must NOT pass any parameter to this function if you want to get the current value) and if its zero then you know that it has been suppressed.
// Custom error handler
function myErrorHandler($errno, $errstr, $errfile, $errline)
    if (
0 == error_reporting () ) {
// Error reporting is currently turned off or suppressed with @
// Do your normal custom error reporting here

For more info on setting a custom error handler see:
For more info on error_reporting see:
M. T.
11.10.2009 18:20
Be aware of using error control operator in statements before include() like this:


 OR die(
"Could not find file.php!");


This cause, that error reporting level is set to zero also for the included file. So if there are some errors in the included file, they will be not displayed.
dsbeam at gmail dot com
2.09.2009 4:22
Though error suppression can be dangerous at times, it can be useful as well.  I've found the following statements roughly equivalent:

     if( isset( $var ) && $var === $something )
     if( @$var === $something )

EXCEPT when you're comparing against a boolean value (when $something is false).  In that case, if it's not set the conditional will still be triggered.

I've found this useful when I want to check a value that might not exist:

     if( @$_SERVER[ 'HTTP_REFERER' ] !== '/www/some/path/file' )

or when we want to see if a checkbox / radio button have been submitted with a post action

     if( @$_POST[ 'checkbox' ] === 'yes' )

Just letting you guys know my findings, :)
19.05.2009 22:46
Error suppression should be avoided if possible as it doesn't just suppress the error that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. This will make debugging a nightmare.

It is far better to test for the condition that you know will cause an error before preceding to run the code. This way only the error that you know about will be suppressed and not all future errors associated with that piece of code.

There may be a good reason for using outright error suppression in favor of the method I have suggested, however in the many years I've spent programming web apps I've yet to come across a situation where it was a good solution. The examples given on this manual page are certainly not situations where the error control operator should be used.
taras dot dot dot di at gmail dot com
12.08.2008 17:29
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
27.05.2008 11:29
NB The @ operator doesn't work when throwing errors as exceptions using the ErrorException class
nospam at blog dot fileville dot net
3.01.2007 20:58
If you want to log all the error messages for a php script from a session you can use something like this:
error($error, $return=FALSE) {
$_SESSION['php_errors'])) {
$_SESSION['php_errors'] = array();    
$_SESSION['php_errors'][] = $error; // Maybe use $php_errormsg
if($return == TRUE) {
$message = "";
$_SESSION['php_errors'] as $php_error) {
$messages .= $php_error."\n";
$messages; // Or you can use use $_SESSION['php_errors']
Hope this helps someone...

13.12.2006 2:52
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.
programming at kennebel dot com
13.10.2006 15:38
To suppress errors for a new class/object:

// 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.
me at hesterc dot fsnet dot co dot uk
3.03.2005 17: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?':

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:

= @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.
frogger at netsurf dot de
26.12.2004 17:19
Better use the function trigger_error() (
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.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)