Consider
$foo = "abcdefg";
echo $foo[0]; //outputs a
So it seems like strings are like array of characters but then why
foreach($foo as $char)
{
echo $char;
}
does not work and gives following Warning ??
Warning: Invalid argument supplied for foreach()
Adding string iteration support to
foreachwas discussed but declined. There were mainly two reasons for this decision:To solve both problems there was a proposal to introduce a
TextIterator, which you pass a string and a charset. That way you can’t accidentally iterate a string and the byte vs character problem doesn’t exist. I’m not sure though what the state of theTextIteratoris currently.