note 9722 added to strlen
| From: | jafo-php at tummy dot com | Date: | Mon, 20 Nov 2000 22:14:12 +0000 |
| Subject: | note 9722 added to strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-7933@lists.php.net to get a copy of this message | ||
I think the confusion on the while loop to traverse the statement is that the user expects it to be
zero terminated. (as in C). In C, the string "blah" contains 5 bytes -- there is a
trailing '\0', so blah[4] == 0.
Of course, having strings zero terminated means that your strings can't contain zeros. In PHP,
it sounds like you have exactly 4 bytes in the string "blah" (as well as some length
indicator), so blah[4] is not a valid index.
The while loop above is just buggy, IMHO. It doesn't work on strings that have an embedded
'\0', or I guess I should say it works in an unanticipated way to some.
Why wouldn't you do:
for ($n = 0; $n < strlen(s); $n++) {
doSomethingWith($s[n]);
}
http://www.php.net/manual/function.strlen.php