-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15. Switch Statements.php
More file actions
87 lines (79 loc) · 1.68 KB
/
15. Switch Statements.php
File metadata and controls
87 lines (79 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// Use the switch statement to select one of many blocks of code to be executed.
// The expression is evaluated once
// The value of the expression is compared with the values of each case
// If there is a match, the associated block of code is executed
// The break keyword breaks out of the switch block
// The default code block is executed if there is no match
Syntax:
// switch (expression) {
// case label1:
//code block
// break;
// case label2:
//code block;
// break;
// case label3:
//code block
// break;
// default:
//code block
// }
// Single Case used
$weekday = 7;
switch ($weekday) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "wadnesday";
break;
case 4:
echo "Thursday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "Enter valide Weekday";
}
// Multiple cases to use the same code block
$weekdaytime = 7;
switch ($weekdaytime) {
case 1:
case 2:
case 3:
case 4:
case 5:
echo "<br> Only Work not for party";
break;
case 6:
echo "<br>Saturday party time";
break;
case 7:
echo "<br> Sunday shoping time";
break;
default:
echo "<br> Enter valide Weekday";
}
// logic example
$age = 19;
switch ($age) {
case ($age >= 18 && $age <= 21):
echo "<br> you are eligible";
break;
case ($age >= 21 && $age <= 30):
echo "<br> you are not eligible";
break;
default:
echo "<br> Enter the valid numbers";
}