 Logical Operators
× Gray Matter
Logical Operators

Defining conditions is the essential part of programming. Your skill to define the conditions tell your programming expertise to others. To define conditions using if else and switch case statement, we require logical operators.

### Operator 'and'

This operator returns true if both conditions are true.

See below example-

``````<?php
\$x = 6;
\$y = 7;
if(\$x == 6 and \$y == 7)
{
echo "Both conditions are true.";
}
?>``````

Output:

Both conditions are true.

### Operator 'OR'

This operator returns true if any one condition is true,

``````<?php
\$x = 6;
\$y = 7;
if(\$x == 6 or \$y == 10){
echo "One condition is true.";
}
?>``````

Output:

One condition is true. We can use '&&' instead of 'and' and '||' instead of 'or'. Both have same functionalities but when you will code for too many conditions then '&&' and '||' will provide accurate result due to their precedence.

### Operator '!'

This operator returns true if condition is false. So we can say that it return the opposit value of its operand.

See below example-

``````<?php
\$x = 6;
if(\$x != 5){
echo "Value of variable x was 6, not 5.";
}
?>
``````

Output:

Value of variable x was 6, not 5.

### Operator 'Xor'

This operator returns true if any one condition become true but not both.

See below example-

``````<?php
\$x = 6;
\$y = 7;
if(\$x == 6 xor \$y == 10){
echo "Only one condition is true.";
}
?>``````

Output:

Only one condition is true.

← Preview

Comparison Operators

Next →

String Operators

NOTES Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

## Courses

• Python
• Game Development with Unity 3D
• Arduino
• PCB & Circuit Designing
• Robotics and Embedded C
• 8051/8052 Embedded Systems Get the Android App Get the iOS App