How does PHP store strings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Taras_96

    #1

    How does PHP store strings?

    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_conten ts($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_conten ts 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($currentB yte != NULL)
    {
    printByte($curr entByte);
    $currentByte = getNextByte($cu rrentByte);

    }

    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

  • Toby A Inkster

    #2
    Re: How does PHP store strings?

    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!

    Comment

    • Taras_96

      #3
      Re: How does PHP store strings?


      On Feb 4, 7:24 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
      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

      Comment

      Working...