PHP - tutorial - 03 - conditional statements, loops

revision:


PHP - conditional statements are used to perform actions based on different conditions.

The "if statement" executes some code if one condition is true.

Syntax:

        if (condition) {
            code to be executed if condition is true;
        }
    

example:

Have a good day!
code:
                <?php
                    $t = date("H");
                    if ($t < "20") {
                    echo "Have a good day!";
                    }
                ?>
            

The "if...else statement" executes some code if a condition is true and another code if that condition is false.

Syntax:

        if (condition) {
            code to be executed if condition is true;
        } else {
            code to be executed if condition is false;
        }
    

example:

Have a nice day!
code:
                <?php
                    $t = date("H");
                    if ($t < "20") {
                    echo "Have a nice day!";
                    } else {
                    echo "Have a good night!";
                    }
                ?>
            

The "if...elseif...else statement"> executes different codes for more than two conditions.

Syntax:

        if (condition) {
            code to be executed if this condition is true;
        } elseif (condition) {
            code to be executed if first condition is false and this condition is true;
        } else {
            code to be executed if all conditions are false;
        }
    

example:

Have a good morning!
code:
                <?php
                    $t = date("H");
                    if ($t < "10") {
                        echo "Have a good morning!";
                    } elseif ($t < "20") {
                        echo "Have a good and splendid day!";
                    } else {
                        echo "Have a good night!";
                    } 
                    ?>
            

The "switch statement" is used to perform different actions based on different conditions.

Use the switch statement to select one of many blocks of code to be executed.

Syntax:

        switch (n) {
            case label1:
              code to be executed if n=label1;
              break;
            case label2:
              code to be executed if n=label2;
              break;
            case label3:
              code to be executed if n=label3;
              break;
              ...
            default:
              code to be executed if n is different from all labels;
        }
    

example:

Your favorite color is red!
code:
                <?php
                    $favcolor = "red";

                    switch ($favcolor) {
                    case "red":
                        echo "Your favorite color is red!";
                        break;
                    case "blue":
                        echo "Your favorite color is blue!";
                        break;
                    case "green":
                        echo "Your favorite color is green!";
                        break;
                    default:
                        echo "Your favorite color is neither red, blue, nor green!";
                    }
                ?>
            

PHP - loops are used to execute the same block of code, as long as a condition is true.

The while loop executes a block of code as long as the specified condition is true.

Syntax:

        while (condition is true) {
            code to be executed;
        }
    

example:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
code:
                <?php
                    $x = 1;

                    while($x <= 5) {
                    echo "The number is: $x <br>";
                    $x++;
                    } 
                ?>
            

The "do...while loop" will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax:

        do {
            code to be executed;
        } while (condition is true);
    

example:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
code:
                <?php
                    $x = 1;
                    do {
                    echo "The number is: $x 
"; $x++; } while ($x <= 5); ?>

The "for loop" is used when you know in advance how many times the script should run.

Syntax:

        for (init counter; test counter; increment counter) {
            code to be executed for each iteration;
          }
    

Parameters:
- init counter : initialize the loop counter value;
- test counter : evaluated for each loop iteration; if it evaluates to TRUE, the loop continues; if it evaluates to FALSE, the loop ends;
- increment counter : increases the loop counter value

example:

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
code:
                <?php
                for ($x = 0; $x <= 10; $x++) {
                    echo "The number is: $x <br>";
                }
                ?>
            

The "foreach loop" works only on arrays, and is used to loop through each key/value pair in an array.

Syntax:

        foreach ($array as $value) {
            code to be executed;
        }
    

example:

red
green
blue
yellow
Peter = 35
Ben = 37
Joe = 43
code:
                <?php
                    $colors = array("red", "green", "blue", "yellow");

                    foreach ($colors as $value) {
                    echo "$value <br>";
                    }

                    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

                    foreach($age as $x => $val) {
                    echo "$x = $val<br>";
                    }
                ?>
            

Break - Continue

The break statement can be used to jump out of a loop.

The number is: 0
The number is: 1
The number is: 2
The number is: 3
code:
                <?php
                    for ($x = 0; $x < 10; $x++) {
                    if ($x == 4) {
                        break;
                    }
                    echo "The number is: $x <br>";
                    }
                ?>
            

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
code:
                <?php
                   for ($x = 0; $x < 10; $x++) {
                    if ($x == 4) {
                        continue;
                    }
                    echo "The number is: $x <br>";
                    }
                ?>
            

Break and continue can also be used in while loops.

The number is: 0
The number is: 1
The number is: 2
The number is: 3
code:
                <?php
                    $x = 0;

                    while($x < 10) {
                    if ($x == 4) {
                        break;
                    }
                    echo "The number is: $x <br>";
                    $x++;
                    }
                ?>