简体   繁体   English

isset($var) 与 @$var

[英]isset($var) vs. @$var

Is this an okay practice or an acceptable way to use PHP's error suppressing?这是使用 PHP 错误抑制的好做法还是可接受的方法?

if (isset($_REQUEST['id']) && $_REQUEST['id'] == 6) {
  echo 'hi';
}

if (@$_REQUEST['id'] == 6) {
  echo 'hi';
}

EDIT:编辑:
I thought so too.我也是这么想的。 The code (and idea) is from friend.代码(和想法)来自朋友。
Thanks for proving me right.谢谢你证明我是对的。 :) :)

Suppressing the errors using @ only suppresses the display of the error, not the creation.使用@ 抑制错误只会抑制错误的显示,而不是创建。 So you get a small performance hit from the error if you don't check isset() first.因此,如果您不先检查isset(),则会从错误中获得很小的性能损失。

It's not a really good practice to use error suppressing.使用错误抑制并不是一个很好的做法。 It's even not a good practice to use $_REQUEST at all.使用 $_REQUEST 甚至都不是一个好习惯。 Just use isset() or !empty() or whatever, don't be lazy.只需使用 isset() 或 !empty() 或其他什么,不要偷懒。

And one more thing, it is a "good practice" to close parenthesis when using isset() :)还有一件事,在使用 isset() 时关闭括号是一个“好习惯”:)

No, it's not really an acceptable practice in my opinion.不,在我看来,这并不是一种真正可以接受的做法。 Apart from the fact that it looks sloppy, custom error handlers are still triggered even when using error suppression.除了看起来很草率之外,即使使用错误抑制,自定义错误处理程序仍然会被触发。

The manual offers more reasons to avoid its use altogether:手册提供了更多完全避免使用它的理由:

Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution.目前,“@”错误控制运算符前缀甚至会禁用将终止脚本执行的严重错误的错误报告。 Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.除此之外,这意味着如果您使用“@”来抑制某个函数的错误,并且它不可用或输入错误,则脚本将立即死在那里,没有任何迹象表明原因。

i always use isset() as it's more specific.我总是使用 isset() 因为它更具体。 Also, i'd use a more specific superglobal variable, so use either $_POST, $_GET, $_SESSION.另外,我会使用更具体的超全局变量,所以使用 $_POST、$_GET、$_SESSION。 Being clear with your code avoids headaches later on :)清楚你的代码可以避免以后的麻烦:)

This is how i run my checks:这是我如何运行我的检查:

if(isset($_POST['id']) && $_POST['id'] == '6')
{
     // do stuff
}

This is pretty thorough checking, since it checks for an existance of a post, then whether my variable is part of the post, and finally if those two pass, it checks to see if my variable is equal to 6.这是非常彻底的检查,因为它会检查帖子是否存在,然后我的变量是否是帖子的一部分,最后如果这两个通过,它会检查我的变量是否等于 6。

Apart from being not a good practice, since @ can chew on really important errors down the call stack, performance penalty is minuscule.除了不是一个好的做法,因为@可以在调用堆栈中咀嚼真正重要的错误,性能损失是微不足道的。

Let's verify this with a benchmark.让我们用一个基准来验证这一点。

<?php
error_reporting(-1);

$limit = 10000;

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    echo !isset($_GET['aaa']) ? '' : $_GET['aaa'];
}
$total = 1000000 * (microtime(true) - $start)/$limit;
echo "With isset: $total μs\n";

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    echo @$_GET['aaa'];
}
$total = 1000000 * (microtime(true) - $start)/$limit;
echo "With @: $total μs\n";

On my not-so-recent computer it outputs:在我最近的电脑上,它输出:

With isset: 0.295 μs
With @: 0.657 μs

μs is a millionth of a second. μs 是百万分之一秒。 Both methods take close to half of a millionth of a second.这两种方法都需要接近百万分之一秒的一半。

One could say, but what if I do this for hundreds or thousands times, will there will be any difference?可以说,但是如果我这样做成百上千次,会有什么不同吗? If you have to do !isset() a million of times, then your program already spent about 0.3 second doing this!如果您必须执行!isset()一百万次,那么您的程序已经花费了大约 0.3 秒的时间! Which means that you shouldn't have been doing that in the first place.这意味着您一开始就不应该这样做。

Nevertheless, @ is a bad practice for anything more complex than a simple array, hence do not use it even if you know that performance difference is insignificant.然而,对于比简单数组更复杂的任何事物, @是一种不好的做法,因此即使您知道性能差异微不足道,也不要使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM