字符串作为PHP中的数组 [英] Strings as arrays in php

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

问题描述

我知道php中的字符串是.....字符串,但是例如我可以做到

I know that strings in php are ..... strings but for example I can do

$str = 'String';
echo $str[0];
echo $str[1];

//result
S
t

echo count($str)
//result
1

为什么我可以像穿过数组那样走过它们,却不能用count来计数? (我知道我可以使用strlen())

Why I can walk trough them like in an array but can't count them with count? (I know that I can use strlen() )

推荐答案

因为它是这样工作的.您可以使用方括号符号访问特定的字节偏移量.但这并不意味着字符串是一个数组,您可以使用期望在其上放置数组的函数. $string[int]substr($string, int, 1)的语法糖,仅此而已.

Because that's how it works. You can access specific byte offsets using bracket notation. But that doesn't mean the string is an array and that you can use functions which expect arrays on it. $string[int] is syntactic sugar for substr($string, int, 1), nothing more, nothing less.

这篇关于字符串作为PHP中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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