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

查看:30
本文介绍了字符串作为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

为什么我可以像数组一样穿过它们,但不能用计数来计算它们?(我知道我可以使用 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天全站免登陆