By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,745 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,745 IT Pros & Developers. It's quick & easy.

How does PHP store strings?

P: n/a
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

Feb 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Taras_96 wrote:
This behaviour leads me to believe that strings in PHP are something
more than just null-terminated byte sequences in PHP.
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
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 4 '07 #2

P: n/a

On Feb 4, 7:24 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Taras_96 wrote:
This behaviour leads me to believe that strings in PHP are something
more than just null-terminated byte sequences in PHP.

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.
Ah hah! Thanks for that, it clears a few things up for me

Taras

Feb 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.