Re: Re: What could the intention be of someone using "\0"in comparison be?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 19.08.2014 16:46, schrieb Toby Hart Dyke:
> 
> But what does "\0" mean, and where is it documented? In this context, it
> clearly means zero, but not false, so the intention is to check for the
> presence of the tested string.
> 
> It seems that the backslash means to treat as a literal zero character,
> which then gets treated as a "true" zero value, rather than a false. I
> get PHP's dynamic typing, but this seems a bit tortuous, and I would
> always use your idiom ( !== false) whic is much more the PHP way.

"\0" is a string consisting of a single NUL-Byte (ASCII code 0).  The
notation is an escape sequence, which are documented in the manual
section about strings[1].

What actually happens in the expression is caused by PHP's type
juggling[2].  strpos() returns either a number or false.  When a number
and a string are loosely compared, the string is converted to a number
first, and then a numeric comparison is done.  When a boolean value is
loosely compared with a string, the string is converted to a boolean
before doing the comparison (where false < true).[3]

When being converted to boolean, the string "0" is converted to false,
and the string "\0" is converted to true.  When being converted to a
number, however, both strings are converted to 0.

Anyway, I would not rely on this particular behavior of casting "\0" to
0 resp. true.  IMHO that is somewhat counter-intuitive.  Comparison with
!== false or maybe > -1 seems to be more sensible here, even though the
latter relies on the fact, that -1 is converted to true, which might be
surprising.

Please do not top post[4].

[1]
<http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double>
[2] <http://php.net/manual/en/language.types.type-juggling.php>
[3] <http://php.net/manual/en/language.operators.comparison.php>
[4]
<http://git.php.net/?p=php-src.git;a=blob_plain;f=README.MAILINGLIST_RULES;hb=HEAD>

-- 
Christoph M. Becker

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux