note 13102 deleted from function.strlen by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:10:56 +0000 |
| Subject: | note 13102 deleted from function.strlen by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189605@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Note that PHP does not need to traverse the string to know its length with strlen(). The length is
an attribute of the array used to store the characters. Do not count on strings being terminated by
a NULL character. This may not work with some character encodings, and PHP strings are binary-safe !
PHP4 included a NULL character after the last position in the string, however, this does not change
the behavior of strlen or the binary safety: this NULL character is not stored.
However PHP4 allows now to reference the position after the end of the string for both read and
write access:
* when reading at that position (e.g. $s[strlen($s)]), you get a warning with PHP3, and you'll
get 0 with PHP4 not returning a warning.
* you can assign it directly in PHP4 with one character to increase the string length by one
character:
<?php
$s[strlen($s)]=65; //append 'A'
$s[]=65; //append 'A'
$s[strlen($s)]=0; //append NUL (stored!)
$s[]=0; //append NUL (stored!)
?>
Such code did not work in PHP3, where the only way to extend the string length was by using a concat
operator.
However, reading or writing past the end of the string, using an array index superior to the current
string length will still raise a warning in PHP4.