PHP $ string {0}与$ string [0]; [英] PHP $string{0} vs. $string[0];

查看:369
本文介绍了PHP $ string {0}与$ string [0];的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In PHP you can access characters of strings in a few different ways, one of which is substr(). You can also access the Nth character in a string with curly or square braces, like so:

$string = 'hello';

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

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

My question is, is there a benefit of one over the other? What's the difference between {} and []?

谢谢.

推荐答案

使用$string[0],PHP6中不推荐使用其他方法(括号)( src )

use $string[0], the other method (braces) is being deprecated in PHP6 (src)

注意:出于相同的目的,也可以使用大括号(如$ str {42}中的字符串)来访问字符串.但是,从PHP 6开始不推荐使用此语法.请改用方括号.

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead.

这篇关于PHP $ string {0}与$ string [0];的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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