note 64321 deleted from language.operators.logical by colder
| From: | colder@php.net | Date: | Fri, 20 Apr 2007 15:37:01 +0000 |
| Subject: | note 64321 deleted from language.operators.logical by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125689@lists.php.net to get a copy of this message | ||
Note Submitter: jhooks
----
>> 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";
}
?>