Tuesday 6 April 2021

Php: Logical operators

Logical operators are used to combine multiple Boolean statements and get the overall final result.

 

Following table summarizes different logical operators supported in php.

 

Operator

Name

Example

Description

and

And

$a and $b

Return true if both $a and $b are true.

or

Or

$a or $b

Return true if either $a or $b is true.

xor

Xor

$a xor $b

Return true if either $a or $b is true, but not both.

!

Not

!a

Return true if $a is false, return false if $a is true.

&&

And

a && b

Return true if both $a and $b are true.

||

Or

a || b

Return true if either $a or $b is true.

 

Let’s see it with an example.

 

logical_operators_demo.php

#!/usr/bin/php

<?php

// Logical And operator
$a = (false and false);
$b = (false and true);
$c = (true and false);
$d = (true and true);

$e = (false && false);
$f = (false && true);
$g = (true && false);
$h = (true && true);

// Logical or
$i = (false or false);
$j = (false or true);
$k = (true or false);
$l = (true or true);

$m = (false || false);
$n = (false || true);
$o = (true || false);
$p = (true || true);

$q = (false xor false);
$r = (false xor true);
$s = (true xor false);
$t = (true xor true);

$u = (! true);
$v = (! false);

echo '(false and false) : ';
var_dump($a);

echo '(false and true) : ';
var_dump($b);

echo '(true and false) : ';
var_dump($c);

echo '(true and true) : ';
var_dump($d);

echo '(false && false) : ';
var_dump($e);

echo '(false && true) : ';
var_dump($f);

echo '(true && false) : ';
var_dump($g);

echo '(true && true) : ';
var_dump($h);

echo '(false or false) : ';
var_dump($i);

echo '(false or true) : ';
var_dump($j);

echo '(true or false) : ';
var_dump($k);

echo '(true or true) : ';
var_dump($l);

echo '(false || false) : ';
var_dump($m);

echo '(false || true) : ';
var_dump($n);

echo '(true || false) : ';
var_dump($o);

echo '(true || true) : ';
var_dump($p);

echo '(false xor false) : ';
var_dump($q);

echo '(false xor true) : ';
var_dump($r);

echo '(true xor false) : ';
var_dump($s);

echo '(true xor true) : ';
var_dump($t);

echo '(!true) : ';
var_dump($u);

echo '(!false) : ';
var_dump($t);
?>

 

Output

$./logical_operators_demo.php 

(false and false) : bool(false)
(false and true) : bool(false)
(true and false) : bool(false)
(true and true) : bool(true)
(false && false) : bool(false)
(false && true) : bool(false)
(true && false) : bool(false)
(true && true) : bool(true)
(false or false) : bool(false)
(false or true) : bool(true)
(true or false) : bool(true)
(true or true) : bool(true)
(false || false) : bool(false)
(false || true) : bool(true)
(true || false) : bool(true)
(true || true) : bool(true)
(false xor false) : bool(false)
(false xor true) : bool(true)
(true xor false) : bool(true)
(true xor true) : bool(false)
(!true) : bool(false)
(!false) : bool(false)

 

&& vs and, || vs or

Php has two different variations of "and" and "or" operators is that they operate at different precedence. && takes precedence over the = operator, whereas = operator takes precedence over 'and' operator.

 

Example

$a = true && false;

Above statement evaluates to false, but if you apply ‘and’ operator to the same snippet, it evaluates to true.

 

$b = true and false;

Since = has greater precedence over 'and', above statement is evaluated like below

 

($b = true) and false;

 

Find the below working application.

 

logical_operator_precedence_demo.php

#!/usr/bin/php

<?php
$a = true && false;
$b = true and false;

echo '$a = ';
var_dump($a);

echo '$b = ';
var_dump($b);
?>

 

Output

$./logical_operator_precedence_demo.php 

$a = bool(false)
$b = bool(true)

 

Logical and, &&, or, || are called as short circuit operators

Logical and, &&: if the first statement in the expression evaluates to false, then php won't evaluates the entire expression.

 

Logical or, ||: if the first statement evaluates to true, then php won't evaluates the entire expression.

 

short_circuit_operator_demo.php

#!/usr/bin/php

<?php

function say_hello()
{
    echo "I am in hello";
    return true;
}

// say_hello() function will not get called in all the below cases
$a = false && say_hello();
$b = false and say_hello();
$c = true || say_hello();
$d = true or say_hello();

echo '$a = ';
var_dump($a);

echo '$b = ';
var_dump($b);

echo '$c = ';
var_dump($c);

echo '$d = ';
var_dump($d);
?>

 

Output

$./short_circuit_operator_demo.php 

$a = bool(false)
$b = bool(false)
$c = bool(true)
$d = bool(true)

 

 

 

 

 

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment