PHP Logical Operator


 

PHP Logical Operator

In this tutorial we will study about logical operator, logical operator are used when we need to check the equality between values. Examples in this tutorial will make it more clear.

In this tutorial we will study about logical operator, logical operator are used when we need to check the equality between values. Examples in this tutorial will make it more clear.

Logical Operators:

This logical operators  are used when we need to check more than one condition, like if more than one condition has to be true then we need to use and (&&) and suppose if any one condition could be true then we need to use or (||) operator. There are many more operators are available and each one has its own importance.

Only reason for keeping two variations of "and" and "or" is that they operate at different precedences.

Example:

<?php

$a=12;

$b=0;

$c=22;

$d=22;

echo "\$a && \$b =";

var_dump ($a && $b);

echo "<br/>";

echo "\$c && \$d =";

var_dump ($c && $d);

echo "<br/>";

echo "\$a || \$b =";

var_dump ($a || $b);

echo "<br/>";

echo "\$c || \$d =";

var_dump ($c || $d);

echo "<br/>";

?>

Output:

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

Example:

<?php

$a=0;

$b=45;

var_dump ($a and $b);

echo "<br/>";

0

$a=10;

$b=45;

var_dump ($a and $b);

1

echo "<br/>";

$a=NULL;

$b=45;

2

var_dump ($a and $b);

echo "<br/>";

$a=10;

3

$b=45;

unset($b);

var_dump ($a and $b);

4

echo "<br/>";

$a=0;

$b=45;

5

var_dump ($a or $b);

echo "<br/>";

$a=10;

6

$b=45;

var_dump ($a or $b);

echo "<br/>";

7

$a=NULL;

$b=45;

var_dump ($a or $b);

8

echo "<br/>";

$a=10;

$b=45;

9

unset($b);

var_dump ($a or $b);

echo "<br/>";

0

 

Output:

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

1

Ads