开发者

Why does @unset give a parse error?

Why can't you hide errors with the @ operator wh开发者_如何学Cen calling unset? The following results in a parse error:

@unset($myvar);


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.


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.


The error suppression operator only works on expressions:

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


Why can't you hide errors with the @ operator when calling unset?

I do not know. But you should not be using the error suppression operator (@) anyway. There are two distinct scenarios:

Developemnt

You want to see all errors right at the moment they happen, preferably with the raw error message that PHP gives you.

Production

You want to let no PHP error message bubble to the user. Instead you want to log the PHP error message and display your own message that a layman can understand.

You cannot achieve this distinction when you use @. You should separate theses scenarios by configuring display_errors, error_reporting and setting an error handler with set_error_handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜