PHP Operators Step By Step Tutorial - Part 11: Logic operator is the operator that is used to compare two values to be yielding of one value so that will get the result of true and false.
The table of Logic Operator
| Operator |
Info |
Example |
Result |
| and or && |
and |
$a and $b or $a && $b |
True if $a & $b is true. |
| or or (||) |
or |
$a or $b
$a || $b
|
True if $a or $b is true. |
| xor |
exclusive or |
$a xor $b |
True if one of $a or $b is correct, but not both. |
| ! |
nor |
!$a |
True if $a is false
False if $a is true.
|
Fur further information, pay attention to the tables of the following:
The table of the Logic Operator Truth.
Operand a |
Operand b |
&& $a && $b |
|| $a || $b |
xor $a xor $b |
! !$a |
| true |
true |
true |
True |
false |
false |
| true |
false |
false |
True |
true |
false |
| false |
true |
false |
True |
true |
true |
| false |
false |
false |
False |
false |
true |
Pay attention to the example of using logic operator at script as following:
File name: logicoperator.php
<html>
<head>
<title>Using Logic Operator</title>
</head>
<body>
<h1>Logic Operator</h1>
<pre>
$q = 16;
$r = 7;
$s = 17;
$t = 17;
</pre>
<?
$q = 16;
$r = 7;
$s = 17;
$t = 17;
$u = ($q > $r) and ($s > $t);
$v = ($q > $r) && ($s > $t);
$w = ($q > $r) or ($s > $t);
$x = ($q > $r) || ($s > $t);
$y = ($q > $r) xor ($s > $t);
$z = ($q < $r) xor ($s > $t);
$a = ($q > $r);
$b = !$a;
printf ("(q > r) and (s > t) = %d <br>",$u);
printf ("(q > r) && (s > t) = %d <br>",$v);
printf ("(q > r) or (s > t) = %d <br>",$w);
printf ("(q > r) || (s > t) = %d <br>",$x);
printf ("(q > r) xor(s > t) = %d <br>",$y);
printf ("(q < r) xor (s > t) = %d <br>",$z);
printf ("(q > r) and (s > t) = %d <br>",$u);
printf ("(q > r) = %d <br>",$a);
printf ("!a = %d <br>",$b);
?>
</body>
</html>