Thursday, 12 May 2011

PHP CONDITIONAL STATEMENTS AND LOOPS - CONDITIONAL STATEMENTS IF ... ELSE, ELSEIF




As in many languages, the control structure if ... else allows the execution of a set of instructions depending on whether a set of conditions are met or not. The PHP syntax is given by:


if (expression)
{set of PHP instructions to execute if the condition given by expression is met}
else
{set of PHP instructions to execute if the condition given by expression is not met}



where expression is the condition to be met (which must be a boolean: TRUE OR FALSE) .

Learn the PHP code:

<?php
$a = 1;
$b = 2;
if ($a == 1 and $b == 2)
{
echo 'The condition is verified'.'<br>';
echo 'We indeed have $a = 1 and $b = 2';
}
else
{
echo 'The condition is not verified';
}
?>

Run the PHP script in your web browser:

If ... then ... else PHP conditional statement

Remarks:

  • Note that equality between two PHP variables is tested using the symbol ==; the symbol = will be only used to define variables.
  • The set of PHP instructions to be executed (both in the case where the condition is met and in the case where it is not) is composed of several instructions separated by semi-colons. The whole set is then put between braces.
  • On the other hand, the condition to check is given by an expression which is put between parentheses and does not end with a semi-colon.
  • You can try to change the value of $a or $b in order to observe the execution of the second set of PHP instructions (since the condition won't be realized anymore).
  • The parentheses containing the condition to check are mandatory; however, the braces giving the sets of instructions to execute are there only to group the instructions belonging to a same block. In this way, they might be removed if (and only if) the set of instructions contains only one instruction (which is the case above only for the case when the condition is not realized). Our code could therefore be rewritten:

    <?php
    $a = 1;
    $b = 2;
    if ($a == 1 and $b == 2)
    {
    echo 'The condition is verified'.'<br>';
    echo 'We indeed have $a = 1 and $b = 2';
    }
    else
    echo 'The condition is not verified';
    ?>
  • In case expression evaluates to False, it is possible to move on to another conditional statement by using elseif; note that, in that case, the second condition will be checked only if the first one evaluated to FALSE:

    <?php
    $a = 3;

    if ($a == 2)
    echo 'It is true that $a = 2'.'<br>';

    elseif ($a == 1)
    echo 'Actually $a = 1';

    else echo '$a is neither 1 nor 2'; ?>
    • Run the PHP script in your web browser: elseif PHP conditional statement Remarks:
      • It is possible to successively use several instances of elseif/else.
      • You can try to change the value of $a in order to execute the different sets of instructions from this PHP conditional statement.
      • PHP CONDITIONAL STATEMENTS AND LOOPS - PHP LOOPS (WHILE, DO ... WHILE)

        While
        Typically, you will use while when you want to repeatedly execute a set of PHP instructions as long as a given condition is met; when the condition is not met anymore, the set of instructions stops being executed and the PHP scripts gets out of the while loop. The syntax is given by: while (expression) {set of PHP instruction to execute while expression is TRUE} where expression is the condition that must hold in order for the set of PHP instructions to keep being repeated.
        Learn the PHP code:
        <?php
        $a = 0;
        while ($a < 10)
        {
        echo $a.'<br>';
        $a = $a + 1;
        }
        ?>
        Run the PHP script on your web browser:
        While loops in PHP

      PHP CONDITIONAL STATEMENTS AND LOOPS - PHP LOOPS (FOR)

      For loops follow the PHP syntax:
      for (expression1; expression2; expression3) {set of PHP instructions to execute if expression2 = TRUE}
      Explanation:
      expression1 is evaluated once (and only once) at the beginning of the loop's execution. Then, expression2 is tested: if it is TRUE, then the set of PHP instructions are executed. Otherwise, the PHP script gets out of the loop and pursue the execution of the script. Eventually, upon each execution of the set of instructions, expression3 is evaluated.
      Learn the PHP code:
      <?php
      for ($i=0; $i < 10; $i = $i+1)
      echo $i; ?>
      Run the PHP script in your web browser:
      for loops in PHP
      Remark:
      Keep in mind that braces can be removed whenever the set of PHP instructions is composed of a single instruction. That's what was done in the previous example.

        PHP CONDITIONAL STATEMENTS AND LOOPS - PHP LOOPS (SWITCH)

        Switch statements follow the PHP syntax: switch ($var) { case value1: instructions 1; break; case value2: instructions 2; break; }
        The values value1, value2, etc ... denote the values against which the PHP variable $var will be checked in each case (starting with value1). If $var is equal to the case value, the corresponding instructions will be executed. When a break statement is met, the switch is interrupted (the PHP script exits the switch statement). Note that the presence of breaks is not compulsory, but if your program is properly written, you know that all your cases will be disjoint; as a result, you know that whenever your variable $var matches a case value, it won't possibly match any other one coming after. Therefore, introducing a break statement will make your PHP script execute faster.
        Remark:
        break can be used in any statement forwhiledo ... while or switch in order to exit the loop/statement. When several structures are nested, the syntax break n (where n is a positive integer) allows the script to exit the first n inner structures and resume the execution at the (n+1)th level. We provide an example of this below:
        Learn the PHP code:
        <?php
        $a = 1;
        $b = 2;
        $c = 3;
        while ($a == 1)
        {
        while ($b == 2)
        {
        while ($c == 3)
        {
        break 2;
        }
        echo 'The script is continued 1 level up'.'
        ';
        $b = 4;
        }
        echo 'The script is continued 2 levels up';
        $a = 4;
        }
        ?>
        Run the script in your web browser:
        PHP break statements
        Remark:
        You can notice that the script was interrupted when the break statement was met; it was resumed two levels up (the script exited two levels of conditional statements) since this was a break 2 statement. If you want the script to exit only one level up, you can try to modify the PHP code above and use a break 1 statement (which is equivalent with a simple break statement). The output in your web browser will be given by: Break statements
        There exist various PHP conditional statements and various PHP loop structures; they are here to help you execute a same set of PHP commands several times in a certain way (using a counter or other conditions). Such structures are essential when programming in PHP and you must be careful to always ascertain that a loop won't run indefinitely (which would make your server crash): there should always be a condition in order to guarantee the exit out of the loop.

No comments:

Post a Comment