简体   繁体   English

PHP中的“ @”符号,例如“ @ $ name [1]”吗?

[英]'@' symbol in php, e.g. '@$name[1]'?

All, 所有,

What is the signification of the '@' symbol in PHP? PHP中“ @”符号的含义是什么? For example, what does it mean in this statement: 例如,此语句表示什么意思:

$sd = (@$argv[1]) ? getdate(strtotime($argv[1])) : getdate();

I understand the ternary operator '?', but I have no idea what the '@' means... 我了解三元运算符'?',但不知道'@'是什么意思...

The code is sample code from the (very good!) cs75.net Harvard extension course. 该代码是(非常好!)cs75.net哈佛扩展课程的示例代码。

Thanks, 谢谢,

JDelage JDelage

The @ symbol suppresses any errors which may be produced in the course of the function (or expression). @ 符号可抑制在函数(或表达式)过程中可能产生的任何错误

PHP supports one error control operator: the at sign (@). PHP支持一种错误控制运算符:at符号(@)。 When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. 当在PHP表达式中添加前缀时,该表达式可能生成的任何错误消息都将被忽略。

Error suppression should be avoided if possible as it doesn't just suppress the error that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. 如果可能的话,应该避免错误抑制,因为它不仅会抑制试图停止的错误,而且还会抑制出乎意料之外的错误。 This will make debugging a nightmare. 这将使调试成为一场噩梦。

The @ supressess any error messages from the expression, that is, if E_NOTICE is set @抑制来自表达式的任何错误消息,即是否设置了E_NOTICE

$argv = Array("Only one object.");
if ($argv[1]) { print "Argv[1] set"; }

would cause an warning, but 会引起警告,但是

$argv = Array("Only one object.");
if (@$argv[1]) { print "Argv[1] set"; }

would simply not print anything. 根本不会打印任何东西。

Keep in mind though, that it's a much better practice to use 不过请记住,使用它是更好的做法

if (!empty($var)) { print "Argv[1] is set and non-false."; }

instead. 代替。

It's the solution for the programmer who can't be arsed checking if $argv[1] is defined and doesn't want to see a warning telling him how lazy he is. 对于那些无法检查$argv[1]并不想看到警告告诉他多么懒惰的程序员来说,这是一个解决方案。 Also known as error suppression . 也称为错误抑制

Others have explained that @ suppresses error messages. 其他人解释说@禁止显示错误消息。 Fun fact from Ilia Alshanetsky's talk on "PHP and Performance": 来自Ilia Alshanetsky关于“ PHP和性能”的演讲的有趣事实:

The error blocking operator is the most expensive character in PHP's alphabet. 错误阻止运算符是PHP字母表中最昂贵的字符。 This seemingly innocuous operator actually performs fairly intensive operations in the background: 这个看似无害的运算符实际上在后台执行相当密集的操作:

$old = ini_set("error_reporting", 0);
action();
ini_set("error_reporting", $old);

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

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