note 64321 added to language.operators.logical
| From: | jhooks at osu1 dot php dot net | Date: | Wed, 12 Apr 2006 10:55:39 +0000 |
| Subject: | note 64321 added to language.operators.logical | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107852@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 difference is explained in the link to on operator precedence.
The '||' operators are evaluated before assignment ('='), whereas the
'or' operators are evaluated after the assignment. In your second example you are telling
PHP to first assign the result of '(choice1 != false ? 'hah' : 'boo')'
to $val, then 'or' it against the rest of the statement. Try the example below and you
will see what I mean (note the extra brackets to enforce precedence):
<?php
define ('choice1', false);
define ('choice2', "dog");
define ('default1', "other");
$val = array();
$val["Test 1"] = (choice1 != false ? 'hah' : 'boo') || (choice2 !=
false ? 'hah2' : 'boo2') || (default1 != false ? 'hah3' :
'boo3');
$val["Test 1b"] = ((choice1 != false ? 'hah' : 'boo') || (choice2 !=
false ? 'hah2' : 'boo2') || (default1 != false ? 'hah3' :
'boo3'));
$val["Test 2"] = (choice1 != false ? 'hah' : 'boo') or (choice2 !=
false ? 'hah2' : 'boo2') or (default1 != false ? 'hah3' :
'boo3');
$val["Test 2b"] = ((choice1 != false ? 'hah' : 'boo') or (choice2 !=
false ? 'hah2' : 'boo2') or (default1 != false ? 'hah3' :
'boo3'));
foreach ( $val as $test => $result ) {
print ( "$test : " ); var_dump ( $result ); print "<br />\n<br
/>\n";
}
?>
----
Server IP: 85.116.0.130
Probable Submitter: 213.218.224.49
----
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/64321
Del: integrated -- http://master.php.net/note/delete/64321/integrated
Del: useless -- http://master.php.net/note/delete/64321/useless
Del: bad code -- http://master.php.net/note/delete/64321/bad+code
Del: spam -- http://master.php.net/note/delete/64321/spam
Del: non-english -- http://master.php.net/note/delete/64321/non-english
Del: in docs -- http://master.php.net/note/delete/64321/in+docs
Del: other reasons-- http://master.php.net/note/delete/64321
Reject -- http://master.php.net/note/reject/64321
Search -- http://master.php.net/manage/user-notes.php