正如我最近了解到的$myArray[$index]
在 PHP 中相当于$myArray{$index}
.
这在上提到过PHP docs http://php.net/manual/en/language.types.array.php。我还在这里发现了一些小讨论:PHP 数组表示法中的大括号 https://stackoverflow.com/questions/8092248/php-curly-braces-in-array-notation.
PHP-FIG http://www.php-fig.org/不包含任何关于哪种方式更可取的建议。
所以,我的问题是——这只是一个品味问题,或者可能是使用一种或另一种语法的一些客观原因?
“方括号表示法”对于数组元素会更加统一和可接受。
在处理数组元素的访问时,“大括号表示法”适用于表达式,但不适用于变量插值。
考虑以下示例:
$myArray = [1,2];
$index = 1;
echo "value at index $index is $myArray[$index]"; // outputs "value at index 1 is 2"
echo "value at index $index is $myArray{$index}"; // will throw "Notice: Array to string conversion"
var_dump($myArray{$index}); // outputs "int(2)"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)