PHP Logical Operators

PHP Logical Operators

हिंदी | PHP Logical Operators in Hindi

हिंदी | Logical Operators of PHP in Hindi


The logical operators are used to perform logical operations based on the given expression. There are six types of Logical Operator in PHP.

Table of PHP Logical Operators

Operator Name Example Result
and And
$x and $y
Returns true if both $x and $y are true
or Or
$x or $y
Returns true if either $x or $y is true, or both are true.
xor Xor
$x xor $y
Returns true if either $x or $y is true. But both should not be true.
&& And
$x && $y
The && operator works similar to the and operator.
|| Or
$x || $y
The || operator works similar to the or operator.
! Not
!$x
Returns true if $x is Not true.

In the above table of the logical operators, we can see that there are two different variations of "and" and "or" operators. The reason for the two different variations of “and” and “or” operators is that they operate at different precedences. (See Operator Precedence.)

  • &&, || – High Precedence
  • and, or – Low Precedence

Logical Operators Examples with the if Statement:

You can use the logical operators and Comparison Operators together. You will see that in the following examples.

and Operator

<?php
$x = true;
$y = true;
$z = false;

#[--TRUE-RESULT
if($x and $y){
    echo "1 - True Condition.<br>";
}
else{
    echo "1 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($x and $z){
    echo "2 - True Condition.<br>";
}
else{
    echo "2 - False Condition.<br>";
}
#--]

//Logical operators and Comparison Operators together
$a = 10;
$b = 20;

#[--TRUE-RESULT
if($a > 5 and $b < 25){
    echo "3 - True Condition.<br>";
}
else{
    echo "3 - False Condition.<br>";
}
#--]

$c = "John";
$d = "Adam";
$e = "Adam";

#[--TRUE-RESULT
if($c !== $d and $d == $e){
    echo "4 - True Condition.<br>";
}
else{
    echo "4 - False Condition.<br>";
}
#--]
?>
Browser Output
1 – True Condition.
2 – False Condition.
3 – True Condition.
4 – True Condition.

or Operator

<?php
$x = true;
$x2 = true;
$y = false;
$z = false;

#[--TRUE-RESULT
if($x or $x2){
    echo "1 - True Condition.<br>";
}
else{
    echo "1 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if($x or $y){
    echo "2 - True Condition.<br>";
}
else{
    echo "2 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($y or $z){
    echo "3 - True Condition.<br>";
}
else{
    echo "3 - False Condition.<br>";
}
#--]

//Logical operators and Comparison Operators together
$a = 10;
$b = 20;

#[--TRUE-RESULT
if($a > 5 or $b < 25){
    echo "4 - True Condition.<br>";
}
else{
    echo "4 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if($a > 5 or $b > 25){
    echo "5 - True Condition.<br>";
}
else{
    echo "5 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($a < 5 or $b > 25){
    echo "6 - True Condition.<br>";
}
else{
    echo "6 - False Condition.<br>";
}
#--]



$c = "John";
$d = "Adam";
$e = "Adam";

#[--FALSE-RESULT
if($c == $d or $d !== $e){
    echo "7 - True Condition.";
}
else{
    echo "7 - False Condition.<br>";
}
#--]
?>
Browser Output
1 – True Condition.
2 – True Condition.
3 – False Condition.
4 – True Condition.
5 – True Condition.
6 – False Condition.
7 – False Condition.

xor Operator

<?php
$x = true;
$x2 = true;
$y = false;
$z = false;

#[--FALSE-RESULT
if($x xor $x2){
    echo "1 - True Condition.<br>";
}
else{
    echo "1 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if($x xor $y){
    echo "2 - True Condition.<br>";
}
else{
    echo "2 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($y xor $z){
    echo "3 - True Condition.<br>";
}
else{
    echo "3 - False Condition.<br>";
}
#--]

//Logical operators and Comparison Operators together
$a = 10;
$b = 20;

#[--TRUE-RESULT
if($a > 5 xor $b > 25){
    echo "4 - True Condition.<br>";
}
else{
    echo "4 - False Condition.<br>";
}
#--]

$c = "John";
$d = "Adam";
$e = "adam";

#[--TRUE-RESULT
if($c !== $d xor $d == $e){
    echo "4 - True Condition.<br>";
}
else{
    echo "4 - False Condition.<br>";
}
#--]
?>
Browser Output
1 – False Condition.
2 – True Condition.
3 – False Condition.
4 – True Condition.
4 – True Condition.

! (Not) Operator

<?php
$x = true;
$y = false;


#[--FALSE-RESULT
if(!$x){
    echo "1 - True Condition.<br>";
}
else{
    echo "1 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if(!$y){
    echo "2 - True Condition.<br>";
}
else{
    echo "2 - False Condition.<br>";
}
#--]


//Logical operators and Comparison Operators together
$a = 10;
$b = 20;

#[--
// $a == $b - that means $a is equal to $b
// $a !== $b - that means $a is not equal to $b
// RESULT IS TRUE BECAUSE $a and $b ARE NOT EQUAL
if($a !== $b){
    echo "3 - True Condition.<br>";
}
else{
    echo "3 - False Condition.<br>";
}
#--]
?>

&& and || Operator

The “&&” operator works similar to the “and” operator, and the “||” operator works similar to the “or” operator.

<?php
$x = true;
$x2 = true;
$y = false;
$z = false;


#[--TRUE-RESULT
if($x && $x2){
    echo "1 - True Condition.<br>";
}
else{
    echo "1 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($x && $y){
    echo "2 - True Condition.<br>";
}
else{
    echo "2 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if($x || $x2){
    echo "3 - True Condition.<br>";
}
else{
    echo "3 - False Condition.<br>";
}
#--]

#[--TRUE-RESULT
if($x || $y){
    echo "4 - True Condition.<br>";
}
else{
    echo "4 - False Condition.<br>";
}
#--]

#[--FALSE-RESULT
if($y || $z){
    echo "5 - True Condition.<br>";
}
else{
    echo "5 - False Condition.<br>";
}
#--]
?>
Browser Output
1 – True Condition.
2 – False Condition.
3 – True Condition.
4 – True Condition.
5 – False Condition.

Leave a Reply

Your email address will not be published. Required fields are marked *