7

If I get a variable and I don't know is it set or not I can write

if (isset($a) && $a > 2)

or I can write

if (@ $a > 2)

which is shorter. Is the second syntax good or not?

Dan
  • 55,715
  • 40
  • 116
  • 154
  • If $a is set and not false or nil – lks128 Aug 31 '10 at 12:34
  • @Andrew that is what I thought but that seems too general for any conditional. Probably better to just use != false or if its a number >0 or if its a string check its length > 0 or something. – Chris Aug 31 '10 at 12:35
  • depending on what you want to allow for $a, [`empty`](http://de2.php.net/manual/en/function.empty.php) might be what you are looking for. – Gordon Aug 31 '10 at 12:45
  • 3
    Think of using '@' as being exactly the same as covering up the 'check engine' light in your car with a piece of tape. Everything looks fine until the motor barfs itself all over the road. – Marc B Aug 31 '10 at 12:47
  • 1
    i'm wondering what would be the purpose of using isset with _variables_ ? You normally do know if your variables are declared or not. – user187291 Aug 31 '10 at 12:51
  • 1
    @stereo: Think of a form submission. An empty field will not get sent client->browser, so `$_REQUEST['name_of_field']` may not necessarily exist, even though the related form field does. – Marc B Aug 31 '10 at 13:26
  • 1) Catching unset variables is a signal of quite poor code. 2) Using `@` is bad for performance 3) Using `isset()` is a common practice – Dan Nov 23 '12 at 10:07

5 Answers5

15

The expression if(@$a) does not check whether the variable is set or not. The @ symbol just surpresses any warnings which is a bad coding style.

Nick
  • 1,904
  • 1
  • 17
  • 34
10

I totally sympathize, but suppressing the error using @ is bad practice.

The error still occurs, it just gets suppressed. It costs microscopical amounts of time that can however accumulate to a lot if done in loops.

Also, you take away the possibility to use "undefined variable" notices to your advantage: As a mechanism to avoid typos.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
3

The first approach is the right approach. The second approach is very bad,. you will end up with errors being suppressed and you wouldn't even know,. and one day you will have errors cropping up that you wouldn't be able to debug its that bad a practice,.

ovais.tariq
  • 2,627
  • 17
  • 12
2

The function isset checks if a variable exists and if it is not null. I'm not quite sure what you want to achieve with the && $a.

The @ operator suppresses the error messages generated by the expression it was prepended to.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
2

Like people already told, the first variant is the way to go. Actually I dislike the (/* */ && $var) variant to check if the variable is empty. I prefer using

if (isset($var) && !empty($var)) {
    /* foo */
}

or just

if (!empty($var)) {
    /* foo */
}
Jan.
  • 1,925
  • 15
  • 17
  • However, if $var is '0', it's not "empty", but still returns true from `empty()`. This WILL bite you in the rump at some point. – Marc B Aug 31 '10 at 13:30
  • Actually not, because I know it.. :-) You could add that isset() misses a variable if it's set to being NULL. So quite often you're in the need to do checks like: `if ((isset($x) && empty($x)) || is_null($x)) { /* set Stored procedure param to String "NULL" */ }` But sure: If you're looking for the digit zero, than you will not use empty()! – Jan. Aug 31 '10 at 13:51