在 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/