note 31677 added to language.types.boolean
| From: | rachel at NO-UBE dot xtreme dot com | Date: | Wed, 30 Apr 2003 02:49:18 +0000 |
| Subject: | note 31677 added to language.types.boolean | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47273@lists.php.net to get a copy of this message | ||
If you aren't sure whether your variable is actually a boolean or not, be very careful with it.
For whatever reason, a true boolean is stored as the number 1, and a false boolean is stored as
nothing, null, empty string, or whatnot. Thus,
$myBool = true ; print( $myBool ) ;
is indistinguishable from
print( "" ) ;
likewise,
&myBool = false ; print( $myBool ) ;
is indistinguishable from
print( 1 ) ;
In addition to the above warnings about boolean evaluation, note that testing for equality is
particularly unintuitive. In particular, "false" the string will equate to true. For
example,
$myTrueBool = true ;
if( $myTrueBool == "true" ) print("it's true!") ;
if( $myTrueBool == "false" ) print("it's false!") ;
if( $myTrueBool == "mushroom" ) print("it's a mushroom!") ;
if( $myTrueBool == 4 ) print("it's the number 4!") ;
will print out
it's true!
it's false!
it's a mushroom!
it's the number 4!
But if $myTrueBool were false, instead, you'd get nothing at all. Caveat Usor.
--
http://www.php.net/manual/en/language.types.boolean.php
http://master.php.net/manage/user-notes.php?action=edit+31677
http://master.php.net/manage/user-notes.php?action=delete+31677
http://master.php.net/manage/user-notes.php?action=reject+31677