☰ Year plan · Communication Technology

The OR gate in VB.NET and PHP

Week 12 · Unit 2 Topics 5–6: OR gate simulation with VB.NET and with PHP · 3 lessons
3

The OR gate on a web page: the || test

ICT Secondary 3 Term 1, Unit 2, Topics 5–6: pp. 48–64
GoalWrite the OR test in PHP with ||, trace the page, and find the fault of a page where && and || were swapped.
New words
|| · PHP’s OR for conditions: true when at least one side is true| · a different operator that works on the bits of whole numbers, for example 6 | 3 is 7swapped operator · the fault of writing || where && belongs, or the other way roundrows that differ · the choices for which two versions of a rule give different results
Explanation

The OR page is the page of Week 10 with one change. The form is the same: two drop-down lists list1 and list2 and an Apply button. The PHP is the same too, and only the test inside the if is new: $choice1 == "on" || $choice2 == "on". The sign || is PHP’s OR for conditions. It is true when at least one side is true, as in the truth table of Week 11. A single | is another operator: it works on the bits of whole numbers, so 6 | 3 gives 7. For conditions write ||. The book prints no code for this page, so the function below is our own version, with the safer checks of Week 10. Now think about faults. If someone writes && on the OR page, or || on the AND page, the lamp is right for off, off and for on, on, but wrong for the two choices where the lists differ. Those two rows tell you which version you are looking at.

AND pageOR page
the test$a == "on" && $b == "on"$a == "on" || $b == "on"
lit foron, on onlyevery choice except off, off
unlit forthree choicesoff, off only
Worked examples
Php
1function lamp(array $post): string {
2    $allowed = ["on", "off"];
3    $first = "off";
4    $second = "off";
5    $state = "unlit";
6    if (isset($post['apply'])) {
7        $first = in_array($post['list1'] ?? "", $allowed, true) ? $post['list1'] : "off";
8        $second = in_array($post['list2'] ?? "", $allowed, true) ? $post['list2'] : "off";
9        $state = ($first === "on" || $second === "on") ? "lit" : "unlit";
10    }
11    return "$first $second $state";
12}
13
14echo lamp([]), "\n";
15echo lamp(['apply' => 'Apply', 'list1' => 'off', 'list2' => 'on']), "\n";
16echo lamp(['apply' => 'Apply', 'list1' => 'off', 'list2' => 'off']), "\n";
17echo lamp(['apply' => 'Apply', 'list1' => 'maybe', 'list2' => 'on']);
Output
off off unlit
off on lit
off off unlit
off on lit
The OR page as a function, with the safer checks of Week 10. The output lines are for a first visit, off and on, off and off, and the text maybe with on.
Php
1var_dump(true || false);
2var_dump(false || false);
3var_dump(1 | 0);
4var_dump(6 | 3);
Output
bool(true)
bool(false)
int(1)
int(7)
The signs || and | on their own.
list1list2AND testOR test
offoffunlitunlit
offonunlitlit
onoffunlitlit
ononlitlit
The swapped-operator fault. The two rows where the lists differ are the only rows that tell the versions apart.
Class activity

A page is meant to be an OR page, but its test is $choice1 == "on" && $choice2 == "on". For which of the four choices does it show the wrong lamp, and what does it show? Then write the corrected test.

Practice
1
What does || mean in PHP?
2
Which line differs between the AND page and the OR page?
3
What does 6 | 3 give, and why?
4
The OR page gets list1 = off and list2 = on. What does the lamp show?