Jump to content

Undefined String Offset.


atrum

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.