PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

String Operators> <Incrementing/Decrementing Operators
Last updated: Fri, 16 May 2008

view this page in

Logical Operators

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 #1 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)


String Operators> <Incrementing/Decrementing Operators
Last updated: Fri, 16 May 2008
 
add a note add a note User Contributed Notes
Logical Operators
Benjamin
29-Feb-2008 06:48
Re:Richard

I show $b printing 1 rather than "banana". Here's how I understand what's going on.

<?php
//"||" has a greater precedence than "or"

$a=0 or $a="avocado"; //evaluated as ($a=0) or ($a="avacado")
//Since $a=0 is false, $a="avocado" is evaluated and $a is assigned the string value "avocado".
echo "$a"; //prints "avocado"
var_dump ($a); // string(7) "avocado"

$b=0 || $b="banana"; // evaluated as $b = (0 || $b = "banana")
echo $b; //prints "1"
var_dump ($b); // bool(true)
?>
Richard
31-Jan-2008 03:22
Re Lawrence:

You sort of can do conditional evaluation:

$a=0 or $a="avocado";
echo "$a";                              #Prints "avocado"

But oddly:

$b=0 || $b="banana";
echo $b;                                 #Prints "banana"
zhustar at gmail dot com
18-Jan-2008 04:02
$a = false xor true;
var_dump($a);  // bool(false)

$a = (false xor true);
var_dump($a);  // bool(true)
pepesantillan at gmail dot com
24-Dec-2007 12:23
worth reading for people learning about php and programming: (adding extras <?php ?> to get highlighted code)

about the following example in this page manual:
Example#1 Logical operators illustrated

...
<?php
// "||" 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);
?>
_______________________________________________end of my quote...

If necessary, I wanted to give further explanation on this and say that when we write:
$f = false or true; // $f will be assigned to false
the explanation:

"||" has a greater precedence than "or"

its true. But a more acurate one would be

"||" has greater precedence than "or" and than "=", whereas "or" doesnt have greater precedence than "=", so

<?php
$f
= false or true;

//is like writting

($f = false ) or true;

//and

$e = false || true;

is the same as

$e = (false || true);

?>

same goes for "&&" and "AND".

If you find it hard to remember operators precedence you can always use parenthesys - "(" and ")". And even if you get to learn it remember that being a good programmer is not showing you can do code with fewer words. The point of being a good programmer is writting code that is easy to understand (comment your code when necessary!), easy to maintain and with high efficiency, among other things.
paranoiq at centrum dot cz
19-Nov-2007 05:00
and, or and xor can be used as conditional constructs:

<?php
// do_that() is executed only if do_this() returns false
if($something) do_this() or do_that();
// $b is assigned to $b, do_that() is executed if $b is false
if($something) $a = $b or do_that();

// do_that() is executed only if do_this() returns true
if($something) do_this() and do_that();
// $b is assigned to $b, do_that() is executed if $b is true
if($something) $a = $b and do_that();

// both do_that() and do_this() are executed..
if($something) do_this() xor do_that();
// .. so the behaviour is same as:
if($something) {
   
do_this();
   
do_that();
}
?>

for understanding what happens if $b is NULL or do_this() returns NULL, read the avbentem's comment on NULL type. generaly speaking, NULL is threated like false in most cases.
peter dot kutak at NOSPAM dot gmail dot com
01-Oct-2007 09:36
$test = true and false;     ---> $test === true
$test = (true and false);  ---> $test === false
$test = true && false;      ---> $test === false
Lawrence
28-Aug-2007 09:04
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');
Andrew
13-Aug-2007 05:49
> <?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. :/
looris at gmail dot com
18-Jun-2007 04:46
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).
eduardofleury at uol dot com dot br
14-Jun-2007 03:16
;;;;;;;;;;;;;;;;;;;;;;;;;
; 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

?>

String Operators> <Incrementing/Decrementing Operators
Last updated: Fri, 16 May 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites