PHP $string{0} 与 $string[0];

标签 php string

在 PHP 中,您可以通过几种不同的方式访问字符串的字符,其中之一是 substr()。您还可以使用大括号或方括号访问字符串中的第 N 个字符,如下所示:

$string = 'hello';

echo $string{0}; // h
echo $string[0]; // h

我的问题是,一个比另一个有好处吗? {} 和 [] 有什么区别?

谢谢。

最佳答案

使用 $string[0],其他方法(大括号)在 PHP 8.0 中已被移除。

对于 strings :

Accessing characters within string literals using the {} syntax has been deprecated in PHP 7.4. This has been removed in PHP 8.0.

对于 arrays :

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.

关于PHP $string{0} 与 $string[0];,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/335205/

相关文章:

php - 如何修复 fsockopen 错误?

php - 如何简化 PHP 中的检查/验证逻辑?

php - 如何使用Memcache加速PHP?

php - 将数组转换为另一种格式

java - StringUtils.isBlank() 与 String.isEmpty()

php - 将日期插入 MySql 数据库时遇到问题

java - 在 Java 中从字符串创建大小图像

Java修剪(): Cleanest way to check null string before trimming?

java - 随机选择字符串后如何从数组中删除字符串? java

string - 如何反转字符串的后缀树(找到它代表的字符串)