views:

1812

answers:

7

I'm new to PHP and I'm confused seeing some examples calling a function with a @ prefix like @mysql_ping().

What is it for? Googling / searching is not much of a help since @ gets discarded and 'alias' is not good enough keyword.

+2  A: 

Googling for "php at symbol" suggests that it asks PHP to not display any error messages that the call causes.

Simon Buchan
+15  A: 

@ suppresses errors, warnings and notices.

You can use it for good purpose if you complement it with a custom error handler or with due check of $php_errormsg variable so you can handle errors properly.

In my experience, this proper usage is not seen very much and is instead used a lot in the bad way, just to hide errors without acting on them.

More info at http://www.php.net/manual/en/language.operators.errorcontrol.php

Vinko Vrsalovic
Well lazy... Let's say it's usefull for a "run once, throw away" script. In any overcase, it's a bad practice.
e-satis
It's terribly useful to suppress error notices if you otherwise have an error handler in place.
eyelidlessness
"It's usually used by lazy programmers who don't want to check error codes" - Completely wrong. @ is exactly how PHP does its "try / catch" http://www.php.net/manual/en/language.operators.errorcontrol.php
Havenard
Very, very removed from a real try/catch though. Standard "if you know what you are doing and have error handlers in place then it's okay" disclaimer. *sigh*
Vinko Vrsalovic
To search google for it, "at sign php" will work.
GZipp
@Havenard its not how php does try catch. Php uses ... wait for it ... try / catch (http://php.net/manual/en/language.exceptions.php). The @ error suppression is just that, error suppression. It can be useful in some circumstances but should be used with care.
Josiah
A: 

Suppress error messages: http://bytes.com/forum/thread10951.html

Lev
+3  A: 

It suppresses the output of error messages. Contrary to another commentator here, I think that it is good programming practice to use it (especially if you are developing a web app, where the output would be mixed in the html of the output page).

Functions like mysql_connect return a resource identifier, or FALSE on errors. Use @mysql_connect(...) and check the return value.

Treb
A: 

Prefixing a function with the a symbol stops it triggering the PHP error handler if an error occurs. Bear in mind that you must do all the error handling yourself if you decide to use it.

$test = @file_get_contents('nonexistant.file');
if(!$test)
{
    die('Failed');
}

A better practice is to turn display_errors off and use custom error handlers (see Error Exception).

Ross
+1  A: 

It suppresses any errors that might otherwise be output.

It is a recipe for pain and hardship, as it inevitably leads to difficulties when an error does occur, you are bound to spend hours tracking down the cause. If the @ operator hadn't been used, then the error would have been found in seconds.

There is no good reason to use it, use the display_errors and error_log ini settings to prevent errors from displaying on a live site, and let them be shown on your dev site.

If there is an error that you don't want to see, you're better off just fixing it than suppressing it!

If it's something in an external lib and outside your control, just write it to the logs, turn off display_errors on production, and live with it. Because there's no telling whether the error you're suppressing now and are happy to live with will ALWAYS be the error that is thrown from there.

@ === BAD

EvilPuppetMaster
A: 

Sometimes it is useful- especially if the admin doesn't want you to play with the php environment or the value isn't important and is mainly cosmetic. Do remember, though; it's a workaround, not a panacea.

[...]

.$foutDate = @filemtime($keyring); /* Don't care, as we've already established file */

$f["date"] = $foutDate;

$f["fullDate"] = date("r", $foutDate);

[...]

Doug