note 9722 deleted from function.strlen by dams

From: Date: Sat, 27 Jul 2002 16:23:18 +0000
Subject: note 9722 deleted from function.strlen by dams
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-33672@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]); }

« previous php.notes (#33672) next »