Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Language Reference : Operators : Logical Operators

Logical Operators

Table 6.7. Logical Operators

Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.


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

// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

// "||" has a greater precedence than "or"
$e = false || true; // $e will be assigned to (false || true) which is true
$f = false or true; // $f will be assigned to false
var_dump($e, $f);

// "&&" has a greater precedence than "and"
$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true
var_dump($g, $h);

The above example will output something similar to:

bool(true)
bool(false)
bool(false)
bool(true)


Code Examples / Notes » language.operators.logical

looris

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


Change Language


Follow Navioo On Twitter
Operator Precedence
Arithmetic Operators
Assignment Operators
Bitwise Operators
Comparison Operators
Error Control Operators
Execution Operators
Incrementing/Decrementing Operators
Logical Operators
String Operators
Array Operators
Type Operators
eXTReMe Tracker