rkaganda Posted April 6, 2009 Share Posted April 6, 2009 <?php $str = 'string'; for($i=0; $i<strlen($str); $i++ ) { echo $str[i]."=".$i."<br>"; } ?> outputs: s=0 s=1 s=2 s=3 s=4 s=5 How come it doesn't echo each element in the string as index'ed? $str[1] would be t, $str[2] would be r.. etc. Quote Link to comment https://forums.phpfreaks.com/topic/152878-trying-to-accsess-string-as-an-array-in-loop/ Share on other sites More sharing options...
wildteen88 Posted April 6, 2009 Share Posted April 6, 2009 $str should be $str[$i] Quote Link to comment https://forums.phpfreaks.com/topic/152878-trying-to-accsess-string-as-an-array-in-loop/#findComment-802839 Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 If I am not mistaken you would also need to use str_split to make the string an array. Quote Link to comment https://forums.phpfreaks.com/topic/152878-trying-to-accsess-string-as-an-array-in-loop/#findComment-802868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.