atrum Posted October 23, 2010 Share Posted October 23, 2010 Ok, I am sure you have all come across this error before. I am trying to understand why this occurs in a straight forward (spelled out for the complete dumb ass) kind of way. I made the most simple script I could come up with that would produce the error, but I am still uncertain as to what causes it, or what the fix is. Here is the script. Using all the letters and numbers available I print the total characters in the string, run a for statement to print out each character in order from the string index, and get the error "Undefined String Offset 36". Which is the total number of characters in the string starting at 0. The test script can be accessed at the following url: http://icca.exiled-alliance.com/classtest.php $test = "abcdefghijklmnopqrstuvwxyz1234567890"; $b = "<br />"; echo $test; echo $b; echo "There are " .strlen($test). " characters in the above string"; echo $b; for($x=0;$x<=strlen($test);$x++){ echo $test[$x]. "-->number $x"; echo $b; } Can anyone clear up why that error pops up from the way I have this scripted. Link to comment https://forums.phpfreaks.com/topic/216664-undefined-string-offset/ Share on other sites More sharing options...
Vitamin Posted October 23, 2010 Share Posted October 23, 2010 Have a look here http://www.phpfreaks.com/forums/index.php?topic=266729.0 Basically the same thing. Link to comment https://forums.phpfreaks.com/topic/216664-undefined-string-offset/#findComment-1125690 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 Change your for() loop comparison to $x < strlen($test). There are 36 characters in the string. If you take the comparison up to where $x = strlen($test), that is 37 iterations, and there is no index 37. Link to comment https://forums.phpfreaks.com/topic/216664-undefined-string-offset/#findComment-1125691 Share on other sites More sharing options...
atrum Posted October 23, 2010 Author Share Posted October 23, 2010 Thanks for the reply every one. And thank you for the simple explanation pikachu2000. This just fixed a lot of my scripts hehe. Link to comment https://forums.phpfreaks.com/topic/216664-undefined-string-offset/#findComment-1125700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.