Appearance
Control structures
Selection
- In PHP, you can use the following (classical) selection structures
if
if else
if elseif
switch
match
(PHP 8+)
- Open course/selection.php
- Change the value of
$scorePHP
(interesting values:4
,15
,12.7
,25.4
,null
) to see the effect on the different selection structures
- Change the value of
php
<article>
<?php
$scorePHP = 9;
echo "<p> \$scorePHP = $scorePHP </p>\n";
?>
</article>
<article>
<h2>if</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations! </p>\n";
}
?>
</article>
<article>
<h2>if else</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations! </p>\n";
} else {
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
}
?>
</article>
<article>
<h2>Ternary operator</h2>
<?php
echo $scorePHP >= 10 ? "<p> You passed the PHP course. Congratulations! </p>\n" : "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
?>
</article>
<article>
<h2>if elseif</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations!</p>\n";
} elseif ($scorePHP >= 8) {
echo "<p> You failed the PHP course, but you may tolerate it! </p>\n";
} else {
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
}
?>
</article>
<article>
<h2>switch</h2>
<?php
switch ($scorePHP) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
break;
case 8:
case 9:
echo "<p> You failed the PHP course, but you may tolerate it! </p>\n";
break;
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
echo "<p> You passed the PHP course. Congratulations!</p>\n";
break;
default:
echo "<p> -- INVALID SCORE -- </p>\n";
}
?>
</article>
<article>
<h2>match</h2>
<?php
$result = match ($scorePHP) {
0, 1, 2, 3, 4, 5, 6, 7 => 'Unfortunately, you failed the PHP course. Better luck next time!',
8, 9 => 'You failed the PHP course, but you may tolerate it!',
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 => 'You passed the PHP course. Congratulations!',
default => '-- INVALID SCORE --',
};
echo "<p> $result </p>\n";
?>
</article>
<article>
<h2>Null coalescing operator</h2>
<?php
echo "<p> Your score for PHP = " . ($scorePHP ?? '-- NO SCORE AVAILABLE --') . "</p>\n";
?>
</article>
<article>
<?php
$scorePHP = 9;
echo "<p> \$scorePHP = $scorePHP </p>\n";
?>
</article>
<article>
<h2>if</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations! </p>\n";
}
?>
</article>
<article>
<h2>if else</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations! </p>\n";
} else {
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
}
?>
</article>
<article>
<h2>Ternary operator</h2>
<?php
echo $scorePHP >= 10 ? "<p> You passed the PHP course. Congratulations! </p>\n" : "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
?>
</article>
<article>
<h2>if elseif</h2>
<?php
if ($scorePHP >= 10) {
echo "<p> You passed the PHP course. Congratulations!</p>\n";
} elseif ($scorePHP >= 8) {
echo "<p> You failed the PHP course, but you may tolerate it! </p>\n";
} else {
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
}
?>
</article>
<article>
<h2>switch</h2>
<?php
switch ($scorePHP) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>\n";
break;
case 8:
case 9:
echo "<p> You failed the PHP course, but you may tolerate it! </p>\n";
break;
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
echo "<p> You passed the PHP course. Congratulations!</p>\n";
break;
default:
echo "<p> -- INVALID SCORE -- </p>\n";
}
?>
</article>
<article>
<h2>match</h2>
<?php
$result = match ($scorePHP) {
0, 1, 2, 3, 4, 5, 6, 7 => 'Unfortunately, you failed the PHP course. Better luck next time!',
8, 9 => 'You failed the PHP course, but you may tolerate it!',
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 => 'You passed the PHP course. Congratulations!',
default => '-- INVALID SCORE --',
};
echo "<p> $result </p>\n";
?>
</article>
<article>
<h2>Null coalescing operator</h2>
<?php
echo "<p> Your score for PHP = " . ($scorePHP ?? '-- NO SCORE AVAILABLE --') . "</p>\n";
?>
</article>
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
- The match expression is 'almost' similar to a switch statement.
- Unlike switch, it will evaluate to a value much like ternary expressions.
- Unlike switch, the comparison is an identity check (
===
) rather than a weak equality check (==
)
- The ternary operator
condition ? 'valueTrue' : 'valueFalse'
provides a shorthand for anif else
statement.- If the
condition
evaluates totrue
, the result of the operator is'valueTrue'
. - Otherwise (
condition
evaluates tofalse
), the result of the operator is'valueFalse'
.
- If the
- During coding, one often has to check whether a
$variable
exists (and is notnull
). This can be achieved by the PHPisset()
function in combination with a ternary operator:isset($variable) ? $variable : 'fallback'
. The null coalescing operator$variable ?? 'fallback'
can be used as a shorthand for the latter statement.
Iteration
- In PHP, you can use the following (classical) iteration structures
for
while
do while
- Open course/iteration.php
- Change the values of
$base
and/or$depthMultiplicationTable
(interesting value:0
) to see the effect on the different iteration structures
- Change the values of
php
<article>
<?php
$base = 7;
$depthMultiplicationTable = 10;
echo "<p> \$base = $base </p>\n";
echo "<p> \$depthMultiplicationTable = $depthMultiplicationTable </p>\n"
?>
</article>
<article class="grid sm:grid-cols-3 gap-4">
<section>
<h2>for</h2>
<ul>
<?php
for ($i = 1; $i <= $depthMultiplicationTable; $i++) {
echo "<li> $i x $base = " . ($i * $base) . "</li>\n";
}
?>
</ul>
</section>
<section>
<h2>while</h2>
<ul>
<?php
$j = 1;
while ($j <= $depthMultiplicationTable) {
echo "<li> $j x $base = " . ($j * $base) . "</li>\n";
$j++;
}
?>
</ul>
</section>
<section>
<h2>do while</h2>
<ul>
<?php
$k = 1;
do {
echo "<li> $k x $base = " . ($k * $base) . "</li>\n";
$k++;
} while ($k <= $depthMultiplicationTable);
?>
</ul>
</section>
</article>
<article>
<?php
$base = 7;
$depthMultiplicationTable = 10;
echo "<p> \$base = $base </p>\n";
echo "<p> \$depthMultiplicationTable = $depthMultiplicationTable </p>\n"
?>
</article>
<article class="grid sm:grid-cols-3 gap-4">
<section>
<h2>for</h2>
<ul>
<?php
for ($i = 1; $i <= $depthMultiplicationTable; $i++) {
echo "<li> $i x $base = " . ($i * $base) . "</li>\n";
}
?>
</ul>
</section>
<section>
<h2>while</h2>
<ul>
<?php
$j = 1;
while ($j <= $depthMultiplicationTable) {
echo "<li> $j x $base = " . ($j * $base) . "</li>\n";
$j++;
}
?>
</ul>
</section>
<section>
<h2>do while</h2>
<ul>
<?php
$k = 1;
do {
echo "<li> $k x $base = " . ($k * $base) . "</li>\n";
$k++;
} while ($k <= $depthMultiplicationTable);
?>
</ul>
</section>
</article>
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
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