Logical Operators in PHP
Notes:
PHP Logical Operators
Logical operators are also known as logical connectives. They are used to connect one or more conditions. They accept Boolean operands, on evaluation they yield the result true or false.
&& / and: Logical and Operator:
If both LHS and RHS operands are true then the result will be true, in all other cases the result will be false
|| / or: Logical or Operator:
If both LHS and RHS operands are false then the result will be false, in all other cases the result will be true
xor : Logical exclusive Or Operator:
If both are same then the result will be false otherwise true
!: Logical not Operator:
If the given operand is false, then the result will be true. Vice versa
Example code:
echo (true && true) ? "true" : "false", "<br/>"; // true
echo (true && false) ? "true" : "false", "<br/>"; // false
echo (false && true) ? "true" : "false", "<br/>"; // false
echo (false && false) ? "true" : "false", "<br/>"; // false
echo "<br/>";
echo (true || true) ? "true" : "false", "<br/>"; // true
echo (true || false) ? "true" : "false", "<br/>"; // true
echo (false || true) ? "true" : "false", "<br/>"; // true
echo (false || false) ? "true" : "false", "<br/>"; // false
echo "<br/>";
echo (true xor true) ? "true" : "false", "<br/>"; // false
echo (true xor false) ? "true" : "false", "<br/>"; // true
echo (false xor true) ? "true" : "false", "<br/>"; // true
echo (false xor false) ? "true" : "false", "<br/>"; // false
echo "<br/>";
echo (!true) ? "true" : "false","<br/>"; // false
echo (!false) ? "true" : "false","<br/>"; // true
echo "<br/>";
echo ((3<4) && (4<5)) ? "true" : "false","<br/>"; // true
echo ((3<4) and (4<5)) ? "true" : "false","<br/>"; // true
echo "<br/>";
echo ((3>4) || (4>5)) ? "true" : "false","<br/>"; // false
echo ((3>4) or (4>5)) ? "true" : "false","<br/>"; // false
echo "<br/>";
echo (!(3<4)) ? "true" : "false","<br/>"; // false
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a