Control structures

Selection

  • In PHP, you can use the following (classical) selection structures
    • if
    • if else
    • if elseif
    • switch
  • 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

 
 
 
 




 
 
 
 
 


 
 
 
 
 
 
 
 


 
 
 


 
 
 
 
 
 
 
 
 
 
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


 
 
 


<div class="margin-3">
    <?php
    $scorePHP = 9;
    echo "<p> \$scorePHP = $scorePHP </p>\n";
    ?>
</div>

<div class="margin-3">
    <h2>if</h2>
    <?php
    if ($scorePHP >= 10){
        echo "<p> You passed the PHP course. Congratulations! </p>";
    }
    ?>

    <h2>if else</h2>
    <?php
    if ($scorePHP >= 10){
        echo "<p> You passed the PHP course. Congratulations!</p>";
    }
    else{
        echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>";
    }
    ?>

    <h2>Ternary operator</h2>
    <?php
    echo $scorePHP >= 10 ? "<p> You passed the PHP course. Congratulations! </p>": "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>";
    ?>

    <h2>if elseif</h2>
    <?php
    if ($scorePHP >= 10) {
        echo "<p> You passed the PHP course. Congratulations!</p>";
    }
    elseif ($scorePHP >= 8){
        echo "<p> You failed the PHP course, but you may tolerate it! </p>";
    }
    else{
        echo "<p> Unfortunately, you failed the PHP course. Better luck next time! </p>";
    }
    ?>

    <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>";
            break;
        case 8: case 9:
            echo "<p> You failed the PHP course, but you may tolerate it! </p>";
            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>";
            break;
        default:
            echo "<p> -- INVALID SCORE -- </p>";
    }
    ?>
    
    <h2>Null coalescing operator</h2>
    <?php
    echo "<p> Your score for PHP = ". ($scorePHP ?? '-- NO SCORE AVAILABLE --') . "</p>";
    ?>
</div>
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

Selection

  • The ternary operator condition ? 'valueTrue' : 'valueFalse' provides a shorthand for an if else statement. If the condition evaluates to true, the result of the operator is 'valueTrue'. Otherwise (condition evaluates to false), the result of the operator is 'valueFalse'.
  • During coding, one often has to check whether a $variable exists (and is not null). This can be achieved by the PHP isset() 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

 
 
 
 
 
 






 
 
 
 
 





 
 
 
 
 
 
 





 
 
 
 
 
 
 




<div class="margin-3">
    <?php
    $base = 7;
    $depthMultiplicationTable = 10;
    echo "<p>\$base = $base </p>\n";
    echo "<p>\$depthMultiplicationTable = $depthMultiplicationTable </p>\n"
    ?>
</div>

<div class="row">
    <div class="col-4">
        <h2>for</h2>
        <div class="margin-3">
            <?php
            for ($i = 1; $i <= $depthMultiplicationTable; $i++) {
                echo "<p><code>$i x $base = " . ($i * $base) . "</code></p>\n";
            }
            ?>
        </div>
    </div>
    <div class="col-4">
        <h2>while</h2>
        <div class="margin-3">
            <?php
            $j = 1;
            while ($j <= $depthMultiplicationTable) {
                echo "<p><code>$j x $base = " . ($j * $base) . "</code></p>\n";
                $j++;
            }
            ?>
        </div>
    </div>
    <div class="col-4">
        <h2>do while</h2>
        <div class="margin-3">
            <?php
            $k = 1;
            do {
                echo "<p><code>$k x $base = " . ($k * $base) . "</code></p>\n";
                $k++;
            } while ($k <= $depthMultiplicationTable);
            ?>
        </div>
    </div>
</div>
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

Iteration

Last Updated: 11/11/2019, 8:47:20 PM