-2

Possible Duplicate:
Reference - What does this symbol mean in PHP?

Code below works, but I'm not sure it is valid:

// It should print $param or $_GET['uuu'] if isset:

function test($param) {
    echo @$_GET['uuu'] ? $_GET['uuu'] : $param;

}

test('param');

Thanx for any suggestions.

Community
  • 1
  • 1
lisek
  • 5
  • 2

3 Answers3

6

If it wasn't valid, it wouldn't work.

It is just really terrible practice to use @ in place of isset() to merely save keystrokes or make your code look cleaner, because that's not what it does. What it does is attempt to access your variable regardless of whether it is set, and suppress the E_NOTICE that inevitably comes up if the variable doesn't exist.

So don't use @ for checking variables as it's not designed for that purpose. Use isset() which does serve that purpose:

echo isset($_GET['uuu']) ? $_GET['uuu'] : $param;
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
3

@ suppresses warning message if $_GET['uuu'] doesn't exist. This is not usually a recommended practice, as it shies away error messages that could be useful to you. For instance, in this example, I wouldn't know whether $_GET['uuu'] is really not set, or there's form error in my html that doesn't send uuu to the server.

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
2

The @ symbol is used just to ignore errors and warnings. It is not good practice to use it. I'd suggest you try NOT to use it.

As for the function, it is valid.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183