1. Combinations of several conditions
Sometimes a condition is build with several sub-conditions. You can combine these atomic conditions together to get the condition you want. There are 7 combinations/functions how to combine two boolean values. These are 3 basic and 4 extended combinations. PHP have implemented 4 of them but all other combinations can be build with these 4 combinations.
2. AND combination
The AND combination is the easiest logical combination.
The result of such a combination is true if and only if both
values used for this operator are true. The language usage
of and is the same like in You are permitted to play outside
if you have done your homework AND have cleaned up your room.
In PHP this combination can be used with the and operator.
<?php
$var = false and false; // results false
$var = false and true; // results false
$var = true and false; // results false
$var = true and true; // results true
$playing = $homework_done and $room_cleaned_up;
?>
The following truth table shows the result of the AND combination.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
In digital eletronics there are special diagramms for these combinations. The AND combination is drawn as followed.
As you see the & character this must be an
and combination. Left are the inputs A
and B and right the output Q.
3. OR combination
A OR combination is the second combination which is
implemented in PHP with the keyword or. The natural language
usage isn't exactly the same as in a programming language. A
pedestrian light is showing green if you press the one or the other
button. But this sounds like the pedestrian light is only switching if
only one button is pressed. Thats not true as the pedestrian light switchs
also if both button are pressed at the same time. And that is how the
OR combination is working. It returns true if one
value or both value is true.
<?php
$var = false or false; // results false
$var = false or true; // results true
$var = true or false; // results true
$var = true or true; // results true
$show_green = $button1 or $button2;
?>
The truth table looks like this.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
In digital eletronics the following graphic symbol is used.
If you add the values of the inputs together you see why the
graphic symbol shows >=1.
4. NOT operator
The NOT operator is an operator which got
only one input. In PHP this its implemented with the
! operator. The value to check must be written
after this operator. The result of this operator is the
negate of the given value. If you have a coin and it falls
on heads it is not on tails.
<?php
$is_heads = !$is_tails;
?>
You have already saw this ! in the compare operators
!= and !==. So you can write these
compare operators as followed.
<?php
$var = $x != $y; // with !=
$var = !($x == $y); // with !(==) (parentheses are needed, otherwise only the $x variable gets negated).
?>
The truth table is a little bit shorter as you have only 1 input. Die Wahrheitstabelle fällt entsprechend kürzer aus.
| Argument (A) | Result (Q) |
|---|---|
| 0 | 1 |
| 1 | 0 |
In digital eletronics the following graphic symbol is used.
The important part is not the 1 but the little
circle at the output line. This is the mentioned negation, in
this case at the output of the graphic symbol. This circle
can also used at the input line, too.
5. NAND combination
Besides the 3 basic combination the NAND is an extended combination. Its a combination build with and AND and a NOT. There doesn't exists such an operator in php but can be build with other operators.
<?php
$check = !($var1 and $var); // AND -> NOT = NAND
$check = !$var1 and $var; // its not an NAND as only $var1 gets negated
?>
In digital eletronics the following graphic symbol is used.
You can see how the AND and NOT operator is build together to get a NAND combination.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
6. NOR combination
The NOR combination is the next extended combination. Its like NAND but with OR instead of AND. As PHP dont have such an operator build in you must build it the same as for NAND.
<?php
$check = !($var1 or $var); // OR -> NOT = NOR
$check = !$var1 or $var; // not an NOR as it only negates $var1
?>
And in digital eletronics its the same as with NAND.
The truth table looks as followed.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
7. XOR combination
The third extended operation is the XOR combination. It works like
OR but exactly one value must be true to get
true.
<?php
$check = ($var1 and !$var2) or (!$var1 and $var2);
?>
As noone wants to write such a combination with the 3 basic combination PHP
implements this combination with the xor operator. The
x stands for exclusive.
<?php
$check = false xor false; // false
$check = true xor false; // true
$check = false xor true; // true
$check = true xor true; // false
$sex_valid = $is_male xor $is_female;
?>
In digital eletronics the following graphic symbol is used.
The =1 shows that the sum of the inputs must be exactly
1 to get the result true.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
8. XNOR combination
The XNOR operation is easy. Its just the negation of XOR.
<?php
$check = !(false xor false); // true
$check = !(false xor true); // false
$check = !(true xor false); // false
$check = !(true xor true ); // true
?>
The graphic symbol looks as followed.
| Argument 1 (A) | Argument 2 (B) | Result (Q) |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
9. Rules for simplify a condition
In digital eletronics there are several rules to simplify a condition/combination. These can be also applied in php scripts.
<?php
$check = true and $var; /* is the same as: */ $check = $var; // the true is useless
$check = false and $var; /* is the same as: */ $check = false; // it is always false
$check = true or $var; /* is the same as: */ $check = true; // it is always true
$check = false or $var; /* is the same as: */ $check = $var; // the false is useless
?>
And there is something like double negation
<?php
$check = !(!$check); // like 'minus times minus'
?>
Much important are the De Morgan's law. In some circumstances you can convert an AND into an OR and the other way round.
<?php
$check = !$var1 and !$var2;
$check = !($var1 or $var2); // the same
$check = !($var1 and $var2);
$check = !$var1 or !$var2; // the same
?>
You exclude the negation and switch the combination. This can be used to increase the readability of your php scripts.
<?php
$skip = !(('.' != $var) and ('..' != $var)); // what do I check? both values shouldn't be two values? I dont get it...
$skip = !(!(('.' == $var) or ('..' == $var))); // resolve double negation
$skip = ('.' == $var) or ('..' == $var); // okay, now I get it
?>