在php中使用@而不是isset

发布于 2024-11-26 21:37:43 字数 122 浏览 1 评论 0原文

在某些情况下,我可以使用 @ 字符而不是使用较长的 isset() 函数吗? 如果没有,为什么不呢?

我喜欢使用它,因为在很多情况下我可以保存几个引号、括号和点。

In some conditions, may I use an @ character instead of using the longer isset() function?
If not, why not?

I like to use this because in a lot cases I can save several quotation marks, brackets and dots.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

耀眼的星火 2024-12-03 21:37:43

我假设您正在谈论 错误抑制运算符比如说 @ 字符,但这并不能替代 isset()

isset() 用于确定给定变量是否已存在于一个程序,以确定使用该变量是否安全。

我怀疑您正在做的是尝试使用该变量,无论它是否存在,但抑制可能由此产生的任何错误。在行的开头使用 @ 运算符告诉 PHP 忽略任何错误并且不报告它。

@ 运算符是“临时设置 < 的简写此表达式的 code>error_reporting(0)”。 isset() 是一个完全不同的构造。

I assume you're talking about the error suppression operator when you say @ character, but that isn't a replacement for isset().

isset() is used to determine whether or not a given variable already exists within a program, to determine if it's safe to use that variable.

What I suspect you're doing is trying to use the variable regardless of its existance, but supressing any errors that may come from that. Using the @ operator at the beginning of a line tells PHP to ignore any errors and not to report it.

The @ operator is shorthand for "temporarily set error_reporting(0) for this expression". isset() is a completely different construct.

把回忆走一遍 2024-12-03 21:37:43

您不应该只使用 @@ 抑制警告。这并不意味着代码是正确的,并且根据您的设置,警告可能仍会添加到您的日志文件中。最好使用 isset 来进行检查。

You shouldn't just use an @. The @ suppresses warnings. It doesn't mean the code is correct, and warnings might still get added to your log file depending on your settings. It is much better to use isset to do the check.

池予 2024-12-03 21:37:43

据我所知,@ 不能替代 isset()。它是一个错误抑制运算符,可以防止在脚本中确实存在错误时显示错误。如果在 PHP 代码中使用,这也是一个非常坏的习惯。

As far as I know @ is no substitution for isset(). Its an error suppression operator which prevents displaying errors in case they do exist in the script. Its also a pretty bad habit if used in PHP code.

被你宠の有点坏 2024-12-03 21:37:43

技术上有效,但有几个原因导致我在创建输出时更喜欢显式isset解决方案,我认为这就是您正在做的事情:

  1. 如果我是新开发人员正在处理您的旧代码,我认识到 isset 惯用语。我知道你想做什么。使用@,要弄清楚你的意图并不那么容易。
  2. 假设您要检查对象的属性是否已设置,例如 $user->name。如果您只是使用错误抑制来查看 name 是否已设置,那么如果 $user 未定义,您将永远不会收到通知。相反,最好运行 isset($user->name) 并且明确,这样,如果 $user 未定义,您将收到错误通知。
  3. 总的来说,错误抑制是一个坏习惯。错误通知很好,您应该尽可能轻松地收到错误通知。在不必要的时候压制它们会导致未来出现问题。

It technically works, but there are a few reasons why I prefer the explicit isset solution when creating output, which I assume is what you're doing:

  1. If I'm a new developer working on your old code, I recognize the isset idiom. I know what you're trying to do. With @, it's not so easy to figure out your intention.
  2. Suppose you want to check if an object's property is set, like $user->name. If you just use error suppression to see if name is set, you will never be notified if $user is undefined. Instead, it's better to run isset($user->name) and be explicit, so that, if $user is undefined, you will be notified of the error.
  3. Error suppression is a bad habit overall. Error notices are good, and you should make it as easy as possible to be notified of errors. Suppressing them when it's not necessary leads to problems in the future.
暗地喜欢 2024-12-03 21:37:43

这取决于您想要做什么。例如,如果您正在执行 var_dump() 或其他调试,并且知道有时您的值不会被设置,我会说在这种情况下这是可以的。

var_dump(@$_REQUEST['sometimesIamSet']);

如果您在这种情况下使用它:

if(@$_REQUEST['something']){
    // do something
}
else{
    // do something else
}

我强烈建议您不要这样做。您应该编写代码来明确执行您想要执行的操作。

if(isset($_REQUEST['something'])){
    // Hurray I know exactly what is going on!
}
else{
    // Not set!
}

在生产中我可以考虑使用 @ 的唯一实例是当你想抛出自己的错误时。例如

$database_connection = @db_connect_function();

if($database_connection === false){
    throw new Exception("DB connection could not be made");
}


Also, look at PaulPRO's answer. If what he is saying is indeed true, your log files could also be logging warnings that you don't know about. This would result in your log files being less helpful during debugging after release.

It depends on what you are trying to do. For instance, if you are performing a var_dump() or other debugging and know that sometimes your value will not be set I'd say in this situation it is ok.

var_dump(@$_REQUEST['sometimesIamSet']);

If you are using it in this case:

if(@$_REQUEST['something']){
    // do something
}
else{
    // do something else
}

I would strongly advise against it. You should write your code to do explicitly what you want to do.

if(isset($_REQUEST['something'])){
    // Hurray I know exactly what is going on!
}
else{
    // Not set!
}

The only instance in production I can think about using @ is when you want to throw your own error. For example

$database_connection = @db_connect_function();

if($database_connection === false){
    throw new Exception("DB connection could not be made");
}


Also, look at PaulPRO's answer. If what he is saying is indeed true, your log files could also be logging warnings that you don't know about. This would result in your log files being less helpful during debugging after release.

北方。的韩爷 2024-12-03 21:37:43

如果没有其他原因,不要使用 @ 代替 isset,因为:

看看这段代码:

echo (@$test) ?: 'default';

如果 $test 是 'something' 那么你会获取'某物'

如果 $test 为空、null 或不存在,那么您将得到 'default'

现在问题来了:

假设'0'FALSE是有效答案?

如果 $test 是 '0'FALSE 那么您将得到 'default' NOT '0' 作为你会想要的。

您应该使用长格式三元:

echo (isset($test)) ? $test : 'default';

不需要更多编码,并且在处理可评估为布尔值 false 的参数时更可靠。

If for no other reason, don't use @ as a substitute for isset because of this:

Look at this code:

echo (@$test) ?: 'default';

If $test is 'something' then you'll get 'something'.

If $test is empty, null or doesn't exist, then you'll get 'default';

Now here's where the problem comes in:

Suppose '0' or FALSE are valid answers?

If $test is '0' or FALSE then you'll get 'default' NOT '0' as you would want.

The long-format ternary is what you should use:

echo (isset($test)) ? $test : 'default';

Not much more coding, and more reliable when it comes to dealing with arguments that can evaluate as boolean false.

情深已缘浅 2024-12-03 21:37:43

@ 运算符还会使代码运行速度变慢,如下所示:
http://php.net/manual/en/language.operators.errorcontrol.php

但正如已经指出的那样,如果发生错误,代码的运行速度就会明显变慢。在这种情况下,使用 isset 而不是 @ 运算符的代码要快得多,如下所述:
http://seanmonstar.com/post/909029460/php-error-suppression-performance

the @ operator also makes your code run slower, as pointed out here:
http://php.net/manual/en/language.operators.errorcontrol.php

But as it's been pointed out, the code only runs measurably slower if an error occurs. In that case, code using isset instead of @ operator is much faster, as explained here:
http://seanmonstar.com/post/909029460/php-error-suppression-performance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文