note 63920 added to language.operators.logical
| From: | jhibbard at gmail dot com | Date: | Mon, 03 Apr 2006 22:51:41 +0000 |
| Subject: | note 63920 added to language.operators.logical | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107253@lists.php.net to get a copy of this message | ||
I have read a few of these responses and quite honestly didn't find one that explained the
differences between the "||" and "OR" operators.
The best way to explain it is with an example.
<?php
echo "TEST 1:";
$choice1 = false;
$choice2 = "dog";
$default = "other";
$val = ($choice1 != false ? 'hah' : 'boo') || ($choice2 != false ?
'hah2' : 'boo2') || ($default != false ? 'hah3' : 'boo3');
echo $val;
echo "<BR /><BR /><BR />TEST 2:";
$choice1 = false;
$choice2 = "dog";
$default = "other";
//$val = $choice1 or $choice2 or $default;
$val = ($choice1 != false ? 'hah' : 'boo1' or ($choice2 != false ?
'hah2' : 'boo2') or ($default != false ? 'hah3' : 'boo3');
//$val = $choice1 or $choice2 or $default;
echo $val;
?>
If you use this code, you will have 2 responses. The first will be
TEST 1:1
and the second will be
TEST 2:boo1
The reason is because how the 2 operators respond. The "||" statement returns boolean
(true or false), thus the return value of 1.
The "OR" statement on the other hand, returns the actual statement on TRUE, thus why we
are getting boo1.
Now, let's give one more example and be done with it.
If we use these statements :
<?php
//Statement ONE with ||
$sql = "SELECT * FROM table";
$rst = myqsl_query($sql) || die(MySQL_Error());
//Statement TWO with OR
$sql = "SELECT * FROM table";
$rst = myqsl_query($sql) OR die(MySQL_Error());
?>
You will notice what I mean. Statement one will return true (or simply 1). Statement 2 will return
the actual result of the query (usually Resource ID #5 or the actual mysql error).
I hope this makes it more clear to the newbie/apprentice who is looking for a deeper explanation of
these logical operators.
----
Server IP: 64.71.164.2
Probable Submitter: 69.61.136.254
----
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.operators.logical.php
Edit -- http://master.php.net/note/edit/63920
Del: integrated -- http://master.php.net/note/delete/63920/integrated
Del: useless -- http://master.php.net/note/delete/63920/useless
Del: bad code -- http://master.php.net/note/delete/63920/bad+code
Del: spam -- http://master.php.net/note/delete/63920/spam
Del: non-english -- http://master.php.net/note/delete/63920/non-english
Del: in docs -- http://master.php.net/note/delete/63920/in+docs
Del: other reasons-- http://master.php.net/note/delete/63920
Reject -- http://master.php.net/note/reject/63920
Search -- http://master.php.net/manage/user-notes.php