note 65965 added to language.types.boolean

From: Date: Wed, 10 May 2006 23:05:25 +0000
Subject: note 65965 added to language.types.boolean
Groups: php.notes 
Request: Send a blank email to php-notes+get-111097@lists.php.net to get a copy of this message
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. ---- Server IP: 64.71.164.2 Probable Submitter: 207.225.24.93 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.types.boolean.php Edit -- http://master.php.net/note/edit/65965 Del: integrated -- http://master.php.net/note/delete/65965/integrated Del: useless -- http://master.php.net/note/delete/65965/useless Del: bad code -- http://master.php.net/note/delete/65965/bad+code Del: spam -- http://master.php.net/note/delete/65965/spam Del: non-english -- http://master.php.net/note/delete/65965/non-english Del: in docs -- http://master.php.net/note/delete/65965/in+docs Del: other reasons-- http://master.php.net/note/delete/65965 Reject -- http://master.php.net/note/reject/65965 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111097) next »