إعلانيمكن أن تكون علامتك التجارية هناعلامتك التجارية هنافي كل صفحة من Darslab · طلاب وأولياء أمور ومعلمونinfo@darslab.comاسأل عن الأسعارالأسعارinfo@darslab.com
☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Operators and the if statement

الأسبوع 12 · الوحدة ٣ «إنشاء صفحات الموقع» (٥) الموضوع الثاني المعاملات والجمل الشرطية في PHP: المعاملات الحسابية والإسناد والمقارنة والمنطقية، if وelse وelseif والشرط المختصر · 3 دروس
3

if, else and elseif

ICT Secondary 2 Term 1, Unit 3, Topic 2: pp. 86–92
GoalWrite if, else and elseif and the one-line if, and trace which branch runs.
New words
conditional statement · code that runs a block only when a condition is trueelse · the block that runs when the if condition is falseelseif · a second condition that is tested only if the first was falsein-line if · the one-line form (condition) ? value1 : value2
Explanation

An if statement runs a block of code only when its condition is true: if ($books >= 10) { … }. Add else for the case when it is false. With elseif you test several conditions in order. PHP checks them from the top and runs only the first branch whose condition is true; the rest are skipped, even if they would also be true. So the order matters: test the largest limit first. The one-line form is written $y = ($x == 1) ? "One" : "Two"; it means: if $x is 1 then $y gets One, else it gets Two. It suits short choices of a value. Use curly brackets for every branch, and remember that in the condition you compare with == and never assign with =. Trace the program by writing, line by line, the value of each variable and which branch ran.

Startbooksbooks >= 10yesnolevel = goldbooks >= 5yesnolevel = silverlevel = startershow the levelEnd
Worked examples
Php
1<?php
2$books = 7;
3if ($books >= 10) {
4    $level = "gold";
5} elseif ($books >= 5) {
6    $level = "silver";
7} else {
8    $level = "starter";
9}
10echo "Mona is a $level reader.";
11?>
Output
Mona is a silver reader.
Mona has 7 books. The first test (7 >= 10) is false, the second (7 >= 5) is true, so the level is silver and the else part is skipped.
Php
1<?php
2$books = 12;
3if ($books >= 10) {
4    $level = "gold";
5} elseif ($books >= 5) {
6    $level = "silver";
7} else {
8    $level = "starter";
9}
10echo "Hana is a $level reader.";
11?>
Output
Hana is a gold reader.
Hana has 12 books. Both 12 >= 10 and 12 >= 5 are true, but PHP runs only the first true branch, so the level is gold. If the tests were in the opposite order, everyone with 5 or more books would be silver.
Php
1<?php
2$x = 2;
3$y = ($x == 1) ? "One" : "Two";
4echo $y;
5?>
Output
Two
The one-line if. With $x = 2 the test $x == 1 is false, so $y gets the second value.
Class activity

Write on paper an if / elseif / else for a school mark: 90 or more is “excellent”, 75 or more is “good”, 50 or more is “pass”, else “try again”. Trace it for the marks 90, 75, 74 and 49. Say why the order of the tests matters.

Practice
1
With $books = 4, which level does the code of the lesson give?
2
If two conditions in an if / elseif chain are true, how many branches run?
3
What does $y = ($x == 1) ? "One" : "Two"; store in $y when $x is 1?
4
What is wrong with if ($x = 1) { … } as a test?