数组表示法中的 PHP 花括号 [英] PHP curly braces in array notation

查看:47
本文介绍了数组表示法中的 PHP 花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd just come across a very weird bit of php code:

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

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

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

It works like an array, but nowhere mentioned in the manual. What is this?

推荐答案

在手册中提到的.{} 只是 [] 的替代语法 § 使用方括号语法访问数组元素.自 PHP 7.4.0 起不推荐使用此方法,并且自 PHP 8.0.0 起不再支持.

It is mentioned in the manual. {} is just an alternative syntax to [] § Accessing array elements with square bracket syntax. This method is deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.

注意:

在 PHP 8.0.0 之前,方括号和花括号可以互换使用来访问数组元素(例如,$array[42] 和 $array{42} 在上面的例子中都可以做同样的事情).大括号语法从 PHP 7.4.0 开始被弃用,从 PHP 8.0.0 开始不再支持.

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.

字符串也是如此§ 字符串访问和字符修改:

可以通过使用方数组括号指定字符串后所需字符的从零开始的偏移量来访问和修改字符串中的字符,如$str[42].出于此目的,可以将字符串视为字符数组. [...]

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. [...]

注意:在 PHP 8.0.0 之前,出于同样的目的,也可以使用大括号访问字符串,如 $str{42}.自 PHP 7.4.0 起,此大括号语法已弃用,自 PHP 8.0.0 起不再受支持.

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 花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆