Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- How does PHP store strings?
February 4, 2007, 7:44 am
rate this thread
I've been told that PHP strings are null terminated. If PHP strings
are in fact null terminated, I'm a bit puzzled how the following code
$filename = 'ucs2_file.txt';
$contents = file_get_contents($filename);
echo "Echoing file contents\n";
The HEX contents of the file are as follows:
FE FF 00 61 00 62 00 63 00 64
FE FF is the byte order mark, 00 61 is the character 'a' encoded in
UCS-2, 00 62 is the character 'b' encoded in UCS-2, and the next two
characters in the file are 'c' & 'd' (encoded in UCS-2) represented by
00 63 and 00 64 respectively.
So, file_get_contents reads the file contents into the string. So I'm
quite sure that after the function call, $contents points to an area
of memory that stores the bytes that were in the file; FE FF 00 61 00
62 00 63 00 64.
On the assumption that strings are null-terminated in PHP, I'm
guessing that the echo function is implemented something like (in
while($currentByte != NULL)
$currentByte = getNextByte($currentByte);
In otherwords, the NULL character tells the function when the end of
the string has been reached. So why, if strings are in fact null
terminated in PHP, does the echo function NOT stop when reaching the
third byte in the aformentioned byte sequence? How does it know that
there are more bytes in the string after the NULL byte? This behaviour
leads me to believe that strings in PHP are something more than just
null-terminated byte sequences in PHP.
Re: How does PHP store strings?
Correct. Internally, PHP stores the integer length of the string as well
as the data itself, so it knows how long the string is, and doesn't get
fooled by NULL bytes part-way along the string.
But unless you're writing compiled extensions to PHP, you really don't
need to know about any of that.
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
* = I'm getting there!
- » ssh on command line: force using a group size (prime size) of 1024 (and no...
- — The site's Newest Thread. Posted in » Secure Shell Forum