Under normal circumstances your application runs the programming lines in sequence. But in many case you need to interfere. Some parts are depending on the status of a condition, or needs to be repeated until a certain condition is met. Here we introduce the term 'compound block'. A compound block is a sequence of statements (program lines) that is executed as a whole withing { and }.


Conditional application flow

if-then-else


In the previous chapters you already have met the if-then-else construction. In general:


if (condition) {

   compound block

} else {

  compound block

}


There are cases where you can have an if condition within an if condition:


if (condition) {

   if (condition) {

      compound block

   } else {

      compound block

   }

} else {

  compound block

}


The indentation that is used to make the code more readable is not mandatory, like in Python. Spaces have no meaning in PHP. But it is good practice to indent blocks so that the readability improves. Sometimes you have more conditions in a row.


if (condition) {

   compound block

} else {

   if (condition) {

      compound block

   } else {

      if (condition) {

         compound block

      } else {

        compound block

      }

  }

}


Although there is indentation, the readability of nested ifs is not optimal. To make the code better readable you can consider using elseif. The code then becomes


if (condition) {

   compound block

} elseif (condition) {

  compound block

} elseif (condition) {

  compound block

} elseif (condition) {

  compound block

} else {                                // finalize with a regular else for the situation that none of the conditions are met.



}


switch


another way to implement more if-elsif constructions is by using a switch. This is especially helpful when you only have to validate a value from a single variable / expression:


switch($var) {

      case 1: some code

                  some code

                  break;


      case 2: some code

                  some code

                  break;


      case 3: some code

                  some code

                  break;


     default:   some code

                  some code

                  break;    

}


The variable does not need to be numeric:


switch($word) {

      case 'yes': some code

                      some code

                      break;


      case 'no'   some code

                     some code

                     break;

      

      default:    some code

                     some code

                     break;    

}

Loop structures


PHP has a number of different loop structures. Loop structures repeats a compound block until a certain condition is met. Although obvious, it is important that you verify that a certain loop will end, otherwise you will have a situation where you application becomes unresponsive because the application never ends. Eventually the webserver will interfere because you consume too much time and the application will be aborted. But it is a situation you must avoid.


while loop

A while loop will initially verify the condition and as long as the expression is true, it will repeat a compound block. So the block is repeated 0 to x times.


$c=read_record();

while( $c != '' ) {

   {

     process record

   }

  $c=read_record;

}

do-while loop

A do-while loop will execute the block and will verify at the end of a condition is met to leave the loop. So this block is repeated 1 to x times.


do {

      $c=read_record();

      process record

    }  while( $c != '' ) 


for loop

The for loop is used when you know the amount of times that a loop must be executed. Although there are constructs to process arrays (for-each) you sometimes needs to keep track of the element number


for ($i=0; $i< 10; $i++) {

  compound block

}


$i gets it's initial value of 0, then at the end of the loop the value will increase until the value is 10. You do not need to start with 0, and you cna use an expressen like $i+=2 if you want to increase $i by 2. But always be aware of the danger


What is wrong with the following sample?


for ($i=0; $i = 9; $i+=2) {

  compound block

}


(if you haven't figured it out, $i gets the values 0, 2, 4, 6, 8, 10... and will never get the value of 9. This loop never ends!


Another dangerous (but valid!) way of a for loop is:


for(;;) {

  compund block

}


This loop also never ends. So there needs to be a construct to leave a loop in these situations. 

aborting loop structures

There are situations where you want to abort a loop because of some unexpected error condition. 


break;


The break statement aborts a loop and jumps to the next line after the compound block


if (condition) {

    statements

    break;

    statements;

}


The application flow continues after } when reaching break;



for ($i=0; $i = 9; $i+=2) {

  compound block

  if($i>15) { break; }

}


Loop will abort.

continue; 


The continue statement will restart the loop at the place of the condition. Especially useful in for loops:


for ($i=0; $i = 9; $i+=2) {

  statements 1;

  if(condition) { continue; }  // the $i will increase and the applciation continues at statements 1

  statements 2;

}


foreach loop


If you have arrays, then sometimes you need to process each element. In the PHsPeed runtime code this is a very common situation. foreach will perform the compound block for each element of the array:


$a=[];

$a[]=1;

$a[]=2;

$a[]=3;

foreach($a as $element) {

    $element will contain 1 the first time the loop is executed, 2 on the next iteration, etc.

}


In the variable section we also have discussed associative arrays where elements have a key:


$a=[];

$a['pet']='dog';

$a['owner']='john';

$a['breed']='boxer';


foreach($a as $key => $value) {

  $key will contain the key of the array, $value the content of the element so

   pet and dog in the first iteration

   owner and john in the second etc.

}