0

All,

What is the signification of the '@' symbol in PHP? For example, what does it mean in this statement:

$sd = (@$argv[1]) ? getdate(strtotime($argv[1])) : getdate();

I understand the ternary operator '?', but I have no idea what the '@' means...

The code is sample code from the (very good!) cs75.net Harvard extension course.

Thanks,

JDelage

JDelage
  • 13,036
  • 23
  • 78
  • 112
  • Dupe, though the SO search isn't exactly helpful with these symbols. http://stackoverflow.com/questions/2279289/what-is-the-meaning-of-in-php-closed – Annika Backstrom Jun 18 '10 at 12:59
  • Darn, I searched for it, but couldn't find anything. The answers are better here, IMHO. – JDelage Jun 18 '10 at 13:54

4 Answers4

9

The @ symbol suppresses any errors which may be produced in the course of the function (or expression).

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

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.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
3

The @ supressess any error messages from the expression, that is, if E_NOTICE is set

$argv = Array("Only one object.");
if ($argv[1]) { print "Argv[1] set"; }

would cause an warning, but

$argv = Array("Only one object.");
if (@$argv[1]) { print "Argv[1] set"; }

would simply not print anything.

Keep in mind though, that it's a much better practice to use

if (!empty($var)) { print "Argv[1] is set and non-false."; }

instead.

MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
1

It's the solution for the programmer who can't be arsed checking if $argv[1] is defined and doesn't want to see a warning telling him how lazy he is. Also known as error suppression.

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
1

Others have explained that @ suppresses error messages. Fun fact from Ilia Alshanetsky's talk on "PHP and Performance":

The error blocking operator is the most expensive character in PHP’s alphabet. This seemingly innocuous operator actually performs fairly intensive operations in the background:

$old = ini_set("error_reporting", 0);
action();
ini_set("error_reporting", $old);
Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52