内容简介:翻译自:https://stackoverflow.com/questions/4441528/is-arraypossibly-missing-key-an-anti-pattern
从 PHP
数组中提取可能缺少的值时,可以使用@吗?例:
$value = @$array['possibly_missing_key'];
预期的行为:
if (isset($array['possibly_missing_key'])) {
$value = $array['possibly_missing_key'];
} else {
$value = null;
}
我想知道,在传播使用模式之前.
@运算符会抑制错误消息,并且使用它可能会为其他错误和意外行为设置代码,最终难以追踪.因此,它肯定是反模式.
因此,我非常喜欢第二位.它使它更清晰
>它可能不存在于数组中,并且
>如果不存在,默认值是什么
为了使它更简洁,您可以使用三元条件运算符?:,如 Mark Baker’s answer 所示.代码略少且符号更多,但其含义已得到公认.
翻译自:https://stackoverflow.com/questions/4441528/is-arraypossibly-missing-key-an-anti-pattern
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning ASP.NET 4 in C# and Vb
Imar Spaanjaars / Wrox / 2010-3-19 / GBP 29.99
This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain from this book, you create a great foundation to ......一起来看看 《Beginning ASP.NET 4 in C# and Vb》 这本书的介绍吧!