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

switch, forms and the Add Term page

الأسبوع 13 · الوحدة ٣ «إنشاء صفحات الموقع» (٦) الموضوعان الثاني والثالث: جملة switch؛ صفحة Add_Term.php: تصميم النموذج، GET وPOST، عناصر النموذج، تضمين صفحتي الترويسة والاتصال · 3 دروس
1

switch: many cases, one choice

ICT Secondary 2 Term 1, Unit 3, Topics 2–3: pp. 93–100
GoalUse switch to choose among many cases, and explain break, fall-through and default.
New words
switch statement · a statement that compares one value with a list of cases and runs the matching codecase · one value to compare with, followed by the code to run when it matchesbreak · leaves the switch; without it the next case also runs (falls through)default · runs when no case matches
Explanation

When one variable can have many values and each needs different code, a chain of if and elseif gets long. A switch is neater: switch ($x) compares $x with each case in turn and starts running at the first match. It keeps running until it meets a break; so break means “leave the switch”. If you forget the break, the code of the next case runs as well: this is called falling through. Sometimes it is useful: stacked cases such as case 1: case 2: case 3: share one block, exactly like if ($a == 1 || $a == 2 || $a == 3). The default case runs when nothing matches. Be careful with one word: break leaves only the switch, but exit ends the whole script, so nothing after it is run. The book says that exit leaves the switch; that is not right in PHP. Always write the variable in the first line as $x = 1; with an equals sign.

Startdayday = 1yesnoChessday = 2yesnoDrawingday = 3yesnoFootballNo clubEnd
Worked examples
Php
1<?php
2$day = 3;
3switch ($day) {
4    case 1:
5        echo "Chess";
6        break;
7    case 2:
8        echo "Drawing";
9        break;
10    case 3:
11        echo "Football";
12        break;
13    default:
14        echo "No club";
15}
16?>
Output
Football
A switch for the club days. With $day = 3 the matching case prints Football, and break leaves the switch. The flowchart above is the same choice written as a chain of tests.
Php
1<?php
2$a = 2;
3switch ($a) {
4    case 1:
5    case 2:
6    case 3:
7        echo "1 or 2 or 3";
8        break;
9    default:
10        echo "other";
11}
12?>
Output
1 or 2 or 3
Stacked cases. With $a = 2 the code starts at case 2 and runs to the break, so it prints the shared sentence. If $a were 5, only default would run.
Php
1<?php
2switch (1) {
3    case 1:
4        echo "one ";
5    case 2:
6        echo "two";
7        break;
8    case 3:
9        echo "three";
10}
11?>
Output
one two
Falling through. With no break after case 1, the switch continues into case 2 and prints both words; the break after case 2 stops it before case 3.
CodeOutput
case 1: echo "A"; break; … echo "B";AB
case 1: echo "A"; exit; … echo "B";A
break and exit are different. After break the script goes on and prints B. After exit the script ends, so B is never printed.
Class activity

Write on paper a switch for the numbers 1 to 4 that prints the name of a season of your choice, with a default that prints “not a season”. Then change it so that 1 and 2 share the same word, and test it with 1, 2, 4 and 9.

Practice
1
What does break do inside a switch?
2
What is printed by switch (1) { case 1: echo "a"; case 2: echo "b"; break; }?
3
When does default run?
4
How is exit different from break?