Logical Operators in PHP

Estudies4you
Logical Operators in PHP

Logical Operators or Relational Operators

The table below lists logical operators along with description and examples.
In the examples below A = 10 and B = 20.


Example:
script: Logical_op.php
<html> <head> <title>Logical Operators</title></head>
<body>
<?php
$a = 22;
$b = 0;
echo "a=".$a."</br>"; echo "b=".$b."</br>";
if( $a && $b ){ echo "AND : Both a and b are true<br/>";
}else{
echo "AND : Either a or b is false<br/>";
}
if( $a and $b ){
echo "AND1 : Both a and b are true<br/>";
} else{
echo “ANDI : Either a or b is false<br/>";
}
if( $a II $b ){
echo "OR : Either a or b is true<br/>";
}else{
echo "OR : Both a and b are false<br/>";
}
if( $a or $b ){
echo "OR1 : Either a or b is true<br/>";
}else{
echo "OR1 : Both a and b are false <br/>”;
}
$a = 10;
$b = 20;
echo "a=".$a."</br>";
echo "b=".$b."</br>";
if( $a ){
echo "Value a : a is true <br/>";
} else{
echo "value a: a is false<br/>";
}
if( $b ){
echo "Value b : b is true <br/>";
} else{
echo "Value b : b is false<br/>";
}
if( !$a ){
echo "Value NOT a : a is true <br/>";
} else{
echo "value value NOT a: a is false<br/>";
}
if( !$b ){
echo "value NOT b : b is true <br/>";
}else{
echo " value NOT b: b is false<br/>";
}
?>
</body>
</html>

OUTPUT:
a=22
b=0;
AND : Either a or b is false
AND1 : Either a or b is false
OR : Either a or b is true
OR1 : Either a or b is true
a = 10
b=20 Value a: a is true
Value b:b is true
Value NOT a: a is false
Value NOT b: b is false


To Top