note 78099 added to language.types.boolean

From: Date: Thu, 27 Sep 2007 16:37:28 +0000
Subject: note 78099 added to language.types.boolean
Groups: php.notes 
Request: Send a blank email to php-notes+get-130856@lists.php.net to get a copy of this message
Note you can also use the '!' to convert a number to a boolean, as if it was an explicit (bool) cast then NOT. So you can do something like: <?php $t = !0; // This will === true; $f = !1; // This will === false; ?> And non-integers are casted as if to bool, then NOT. Example: <?php $a = !array(); // This will === true; $a = !array('a'); // This will === false; $s = !""; // This will === true; $s = !"hello"; // This will === false; ?> To cast as if using a (bool) you can NOT the NOT with "!!" (double '!'), then you are casting to the correct (bool). Example: <?php $a = !!array(); // This will === false; (as expected) /* This can be a substitute for count($array) > 0 or !(empty($array)) to check to see if an array is empty or not (you would use: !!$array). */ $status = (!!$array ? 'complete' : 'incomplete'); $s = !!"testing"; // This will === true; (as expected) /* Note: normal casting rules apply so a !!"0" would evaluate to an === false */ ?> ---- Server IP: 64.71.164.2 Probable Submitter: 208.54.15.127 ---- Manual Page -- http://www.php.net/manual/en/language.types.boolean.php Edit -- https://master.php.net/note/edit/78099 Del: integrated -- https://master.php.net/note/delete/78099/integrated Del: useless -- https://master.php.net/note/delete/78099/useless Del: bad code -- https://master.php.net/note/delete/78099/bad+code Del: spam -- https://master.php.net/note/delete/78099/spam Del: non-english -- https://master.php.net/note/delete/78099/non-english Del: in docs -- https://master.php.net/note/delete/78099/in+docs Del: other reasons-- https://master.php.net/note/delete/78099 Reject -- https://master.php.net/note/reject/78099 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130856) next »