AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Communication Technology

Operators and the if statement

Week 12 · Unit 3 Creating Site Pages (5), Topic 2 Operators and conditional statements in PHP: arithmetic, assignment, comparison and logical operators, if, else, elseif and the in-line if · 3 lessons
2

Comparing and combining conditions

ICT Secondary 2 Term 1, Unit 3, Topic 2: pp. 86–92
GoalCompare values and combine conditions with && || !, including the boundary between > and >=.
New words
comparison operator · a sign such as == or >= that compares two values and gives true or falselogical operator · && (and), || (or) and ! (not), which combine true and false valuescondition · an expression that is either true or falseboolean · a value that is true or false
Explanation

A condition is a question whose answer is true or false. Comparison operators ask it: == equal, != not equal, > greater, >= greater or equal, < smaller, <= smaller or equal. Do not mix up = and ==: a single = assigns, and a double == compares. The sign === compares the value and the type, so "5" == 5 is true but "5" === 5 is false. Logical operators combine conditions. && means and: both must be true. || means or: at least one must be true. ! means not: it flips true and false. The truth table below lists every case for two values X and Y. The words and and or also exist, but they bind more loosely than =, so $t = true and false gives $t the value true, which surprises people; use && and ||. The most frequent mistake in conditions is the boundary: “5 books or more” is >= 5, while > 5 leaves out exactly 5.

XYX∨YX∧Y¬X
11110
10100
01101
00001
Worked examples
Php
1<?php
2$books = 5;
3var_dump($books >= 5, $books > 5);
4?>
Output
bool(true)
bool(false)
The boundary. A member with exactly 5 books: is “5 books or more” true? Yes, $books >= 5 is true. Is “more than 5 books” true? No, $books > 5 is false.
Php
1<?php
2var_dump("5" == 5, "5" === 5);
3?>
Output
bool(true)
bool(false)
Comparing the value and the type. The text "5" and the number 5 are equal in value, so == says true; the types differ, so === says false.
Php
1<?php
2var_dump(true || false, true && false, !true);
3$t = true and false;
4var_dump($t);
5$u = (true && false);
6var_dump($u);
7?>
Output
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
Combining conditions: true || false is true, true && false is false, and !true is false. The loose word and gives a surprise: $t = true and false stores true in $t, while the safe form with && stores false in $u.
Class activity

A pool lets a child in free if he is under 6 or over 65. On paper write the condition with ||, then test it with the ages 6, 7, 65 and 66, and write true or false for each. Say why the boundary values matter.

Practice
1
What is the difference between = and ==?
2
What do true && false and true || false give?
3
Which test is true for a member with 10 books: $books > 10 or $books >= 10?
4
Why is && better than the word and when you store a result in a variable?