|
Posted by Taras_96 on 02/04/07 07:44
Hi everyone,
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
can work:
$filename = 'ucs2_file.txt';
$contents = file_get_contents($filename);
echo "Echoing file contents\n";
echo $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
pseudo-code):
while($currentByte != NULL)
{
printByte($currentByte);
$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.
Thanks
Taras
[Back to original message]
|