|
Logical OperatorsTable 6.7. Logical Operators
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.) Example 6.6. Logical operators illustrated<?php The above example will output something similar to: bool(true) Code Examples / Notes » language.operators.logicallooris
Please note that while you can do things like: <?php your_function() or die("horribly"); ?> you can't do: <?php your_function() or return "whatever"; ?> (it will give you a syntax error). lawrence
Note that PHP's boolean operators *always* return a boolean value... as opposed to other languages that return the value of the last evaluated expression. For example: $a = 0 || 'avacado'; print "A: $a\n"; will print: A: 1 in PHP -- as opposed to printing "A: avacado" as it would in a language like Perl or JavaScript. This means you can't use the '||' operator to set a default value: $a = $fruit || 'apple'; instead, you have to use the '?:' operator: $a = ($fruit ? $fruit : 'apple'); eduardofleury
;;;;;;;;;;;;;;;;;;;;;;;;; ; P1 P2; And; OR ; XOR ; ;;;;;;;;;;;;;;;;;;;;;;;;; ; V V ; V ; V ; F ; ; V F ; F ; V ; V ; ; F V ; F ; V ; V ; ; F F ; F ; F ; F ; ;;;;;;;;;;;;;;;;;;;;;;;;; <?php $a = 2; $b = 3; $c = 6; print !($a > $b && $b < $c);// true print (($a > $b) and ($b < $c));// false print ($a == $b or $b < $c); // true print $a == $b || $b < $c; // true $x = $a < $b; //$x = true $y = $b === $c; //$y = false print $x xor $y; // true ?> andrew
> <?php > your_function() or return "whatever"; > ?> doesn't work because return is not an expression, it's a statement. if return was a function it'd work fine. :/ peter dot kutak
$test = true and false; ---> $test === true $test = (true and false); ---> $test === false $test = true && false; ---> $test === false |