carter
carter

Reputation: 1

About php's string in "\0" and ""?

<?php
    $str1 = "";                    
    $str2 = "\0";                  
    var_dump(ord($str1), ord($str2));
    var_dump(strlen($str1), strlen($str2), $str1 === $str2);
    debug_zval_dump($str1, $str2);
?>

Result:


int(0)
int(0)
int(0)
int(1)
bool(false)
string(0) "" refcount(2)
string(1) "" refcount(2)

Why the results are not consistent? This is why ?
Who can answer me.3Q~

Upvotes: 0

Views: 9754

Answers (2)

Chris Seufert
Chris Seufert

Reputation: 869

Looks correct, ord() is expecting a character to be passed, and because you pass an empty string, it just assumes a NULL character (\0). Php strings are not null terminated, you can have perfectly legal strings with null characters within them.

To PHP a '\0' is just a string with one character in it.

Edit:

A PHP string is stored as list of all the characters in the string, and the total length. This allows PHP to store any character value, from 0 to 255. C Strings use the NULL character to determine where the string ends, PHP only uses the length to determine how long the string is.

Upvotes: 1

mattbell87
mattbell87

Reputation: 575

Since PHP is built on C and null-bytes (\0) denote the end of a string in C, I would say it's something to do with that. PHP.net has an article on null bytes and the issues they can cause:

http://php.net/manual/en/security.filesystem.nullbytes.php

Upvotes: 0

Related Questions