Strings as arrays in php

后端 未结 2 1774
粉色の甜心
粉色の甜心 2020-12-03 18:09

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         


        
2条回答
  •  情书的邮戳
    2020-12-03 18:49

    Because strings are not arrays. They allow you to find letters byte offsets (which aren't necessarily letters in a multi-byte character string) using the same syntax for your convenience, but that's about it.

    Arrays can have keys as well and can be sorted. If strings were full arrays, you could give each letter a key, or sort the letters alphabetically using one of the array functions.

    Long story short: a string is not an array, even when a tiny part of their syntax is similar.

提交回复
热议问题