Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does @unset give a parse error?

Tags:

php

parsing

Why can't you hide errors with the @ operator when calling unset? The following results in a parse error:

@unset($myvar);
like image 887
dynamic Avatar asked Jan 12 '11 23:01

dynamic


3 Answers

The @ operator only works on expressions, and unset is a language construct, not a function. See the manual page for more information:

Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include() calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.

like image 78
mfonda Avatar answered Nov 08 '22 22:11

mfonda


You can hide errors by prefixing @ to functions/statements. However unset is a language construct, therefore it doesn't support the @-rule.

The good thing is that unset() never fails even if the variable didn't exist to begin with, so this shouldn't be necessary.

As nightcracker mentionned though, using @ is pretty bad practice.

like image 5
Seldaek Avatar answered Nov 08 '22 23:11

Seldaek


The error suppression operator only works on expressions:

unset is a language construct and not a function, so @ cannot be used.

like image 4
Hamish Avatar answered Nov 08 '22 23:11

Hamish