note 110630 added to language.operators.logical
| From: | m_lajos at hotmail dot com | Date: | Wed, 14 Nov 2012 19:00:12 +0000 |
| Subject: | note 110630 added to language.operators.logical | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191933@lists.php.net to get a copy of this message | ||
Watch out when working with booleans!
<?php
$x = true;
$y = (/* some condition, that returns boolean true or false */);
$x = $x && $y; // $x remains bool( true or false )
$x = $x & $y; // $x becomes int( 0 or 1 )
// let's restart...
$x = true;
// Let's say, you want to shorthand the upper codes in for example a form validation code:
$x &= $y; // same as $x = $x & $y; | this will make $x an int!
// There is no shorthand for && sign addition though, like below:
$x &&= $y;
// The same is with the other logic opeator: or( || )
// xor doesn't even have a doubled version: ^^
// To overcome this absence of logic operators simply add another line at the end which will
transform your int back to boolean:
$x = (bool)$x;
?>
----
Server IP: 69.147.83.199
Probable Submitter: 178.48.17.65
----
Manual Page -- http://www.php.net/manual/en/language.operators.logical.php
Edit -- https://master.php.net/note/edit/110630
Del: integrated -- https://master.php.net/note/delete/110630/integrated
Del: useless -- https://master.php.net/note/delete/110630/useless
Del: bad code -- https://master.php.net/note/delete/110630/bad+code
Del: spam -- https://master.php.net/note/delete/110630/spam
Del: non-english -- https://master.php.net/note/delete/110630/non-english
Del: in docs -- https://master.php.net/note/delete/110630/in+docs
Del: other reasons-- https://master.php.net/note/delete/110630
Reject -- https://master.php.net/note/reject/110630
Search -- https://master.php.net/manage/user-notes.php