php – @ $array [‘possible_missing_key’]是反模式吗?

栏目: PHP · 发布时间: 6年前

内容简介:翻译自: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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Semi-Supervised Learning

Introduction to Semi-Supervised Learning

Xiaojin Zhu、Andrew B. Goldberg / Morgan and Claypool Publishers / 2009-6-29 / USD 40.00

Semi-supervised learning is a learning paradigm concerned with the study of how computers and natural systems such as humans learn in the presence of both labeled and unlabeled data. Traditionally, le......一起来看看 《Introduction to Semi-Supervised Learning》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具