Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Logical combinations

  1. Combinations of several conditions
  2. AND combination
  3. OR combination
  4. NOT operator
  5. NAND combination
  6. NOR combination
  7. XOR combination
  8. XNOR combination
  9. Rules for simplify a condition

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.

Truth table of AND
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.

AND combination as electronic symbol Img.:AND combination as electronic symbol

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.

Truth table of the or combination
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.

OR combination as graphic symbol Img.:OR combination as graphic symbol

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.

Truth table of the NOT operator
Argument (A) Result (Q)
0 1
1 0

In digital eletronics the following graphic symbol is used.

NOT operation as a graphic symbol Img.:NOT operation as a graphic symbol

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.

NAND combination as graphic symbol Img.:NAND combination as graphic symbol

You can see how the AND and NOT operator is build together to get a NAND combination.

Truth table of the 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.

NOR operation as graphic symbol Img.:NOR operation as graphic symbol

The truth table looks as followed.

Truth table of the NOR operator
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.

XOR operation as graphic symbol Img.:XOR operation as graphic symbol

The =1 shows that the sum of the inputs must be exactly 1 to get the result true.

Truth table of the XOR operator
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.

XNOR-Verknüpfung als Schaltzeichen Img.:XNOR-Verknüpfung als Schaltzeichen
Truth table of the XOR operator
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
?>

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!