RE: binary zero evaluates as true!?

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

 



On 09 June 2006 04:11, D. Dante Lorenso wrote:

> Jon wrote:
> > The second thing that needs to be said here is simply a re-statement
> > of the original, this time more verbosely.  The list of things in
> > php that evaluate as false is as follows:
> > 
> > * the boolean FALSE itself
> > * the integer 0 (zero)
> > * the float 0.0 (zero)
> > * the empty string, and the string "0"
> > * an array with zero elements
> > * an object with zero member variables (PHP 4 only)
> > * the special type NULL (including unset variables)
> > * SimpleXML objects created from empty tags
> > 
> > now, all i'm saying is that if, of all ungodly things, the literal
> > string "0" and empty arrays evaluate to false, isn't it appropriate
> > to expect a binary zero to evaluate to false as well?  Maybe i still
> > don't get it.   :(
> 
> Jon,
> 
> You are correct about the behavior you are observing, sorry I doubted
> your ability/methods.  Here is a much shorter example of what you are
> trying to do: 
> 
> <?php
> // start with int '0'
> $as_int = (int) 0x0;
> var_dump($as_int);
> 
> // convert into to binary string
> $as_binstr = (string) pack('C', $as_int);
> var_dump($as_binstr);
> 
> // cast binary string as boolean
> $as_bool = (boolean) $as_binstr;
> var_dump($as_bool);
> > 
> 
> // int(0)
> // string(1) "!~unprintable~!"
> // bool(true)
> 
> You want to know WHY a single byte of all 8 bits set to '0' does not
> evaluate as FALSE.  I don't know the answer,

See above definition of what evaluates to FALSE.  The string you have is a one-character string containing the character NUL (0x00).  It is not the empty string, and it is not the string "0".  Ergo it is TRUE, QED.

> Oddly, if you start with this in the example above:
> 
>     $as_int = (int) 48;
> 
> Then, the is_bool will be false!

Well, that makes sense.  48 is the ASCII code for the character '0', so packing it in the above fashion will result in the string "0".  Which is by definition FALSE.  Nothing odd there.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux