gpt4 book ai didi

数组表示法中的 PHP 大括号

转载 作者:IT王子 更新时间:2023-10-29 01:10:34 26 4
gpt4 key购买 nike

我刚刚遇到了一段非常奇怪的 php 代码:

$oink{'pig'} = 1;
var_dump($oink);

$oink{'pig'} = '123123';
echo $oink{'pig'}; /* => 123123 */
echo $oink['pig']; /* => 123123 */

它像数组一样工作,但手册中没有提及。这是什么?

最佳答案

在手册中提到的。 {} 只是 [] § Accessing array elements with square bracket syntax 的替代语法.自 PHP 7.4.0 起不推荐使用此方法,自 PHP 8.0.0 起不再支持

Note:

Prior to PHP 8.0.0, square brackets and curly braces could be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} would both do the same thing in the example above). The curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.

字符串 § String access and modification by character 也是如此:

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. [...]

Note: Prior to PHP 8.0.0, strings could also be accessed using braces, as in $str{42}, for the same purpose. This curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.

关于数组表示法中的 PHP 大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8092248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com