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, but my theory is as follows:
Perhaps binary strings are not a normal everyday kind of string. If you
have a binary string, you probably got it from a binary place like a
file or stream. If you read a file and that file contains one byte and
that byte is all 00000000, then how is that FALSE when you got 1 byte
not 0 bytes. I might believe that a binary string should always be TRUE
if the length is greater than ZERO bytes in length. Since binary files
are not meant to be looked at, perhaps length is all that determines
their truth?
Oddly, if you start with this in the example above:
$as_int = (int) 48;
Then, the is_bool will be false! Ok, now I'm with you about odd
behavior and wanting a better answer ;-) You can't really use the
bitwise operators on your binary string either. From the manual:
"Bitwise operators allow you to turn specific bits within an integer
on or
off. If both the left- and right-hand parameters are strings, the
bitwise
operator will operate on the characters' ASCII values."
I think the best bet would be to unpack the binary string into a format
that PHP will play nicely with. Most likely if you are looking at a
single byte, unpacking as an int is the way to go.
Dante
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php