note 65965 deleted from language.types.boolean by colder
| From: | colder@php.net | Date: | Tue, 17 Oct 2006 18:48:47 +0000 |
| Subject: | note 65965 deleted from language.types.boolean by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118659@lists.php.net to get a copy of this message | ||
Note Submitter: bnab at southphlattewebdesign dot com
----
Assignment of a boolean value:
I beat my head on this one: Depending upon how the boolean value is assigned, it determines the
actual boolean value on a FALSE value - integer, empty string etc...
Take the following example:
<?php
$x;
$i = true;
echo $i.' I should be true or 1<br><br>'; //value = 1
$i = false;
echo $i.' I should be false or 0<br><br>'; //value = ''
$i = isset($x);
echo $i.' I should be false, as X has no value<br><br>'; //value =
''
$x = 'a string';
$i = preg_match('/str/',$x);
echo $i.' I should be true on match<br><br>'; //value = 1
$i = preg_match('/trinsg/', $x);
echo $i.' I should be false on no match'; //value = 0
?>
The preg_match is false on the second time around, and thus is now assigned the boolean false value
of 0. While in trying to create a boolean literal with value false, it gets assigned as an empty
string false value. Further, I run the isset check and it assigns the empty string, while
preg_match assigns the 0 value.
Maybe others grasped this simple little difference in the fact that you need to evaluate on the
boolean itself, rather than the value as depending upon the assignment method, the value for FALSE
can have one of several true values.