Skip to main content

Posts

Showing posts with the label php control flow

PHP Break and Continue

              With iterative control statements, PHP provides two means of manually interrupting the flow of the loops. These two commands include break (which has already been used in examples up to this point) and  continue.  The following example demonstrates how these two loop interrupts can be used in iterative control statements: <?php     for ($test=1; $test < 10; $test = $test + 1) {         if ($test==3) continue;         if ($test==6) break;         echo "The test number is $test<br>";     } ?> The following output would be displayed when this PHP script was run: The test number is 1 The test number is 2 The test number is 4 The test number is 5               When the  $test variable was equal to 3, the script skipped the remain code and performed the next  iteration of the for function.  This resulted in the number 3 not being printed. In addition, when the $test  variable was equal to 6, the break function ended the for function

PHP FOR STATEMENT

FOR The for function is the more complex sibling of the while function and provides a more streamline and  complex looping mechanism.  The for function takes three expressions; the first expression is evaluated  by default at the first iteration of the loop, the second expression is evaluated at the beginning of each  iteration (and determines if the loop will continue) and the third expression is evaluated at the conclusion of  each loop.  Any of the expressions can be empty and the logic that would take place in the expression can  be substituted in the body of the function itself.  The statement list within the for code body can consist of  one or more statements.  for (expression1; expression2; expression3)  {      -- statements that execute while the expressions evaluates false  }  The following examples demonstrate how the for iterative control statement can work:  <?php      for ($ctemp = 0; $ctemp <= 20; $ctemp = $ctemp + 1) {          $ftemp = 32 + $ctem

PHP Control Flow (if...elseif....else)

        PHP starts with many strengths in its logical and bitwise operators. PHP then extends the ideas of C, Java and Perl control of flow and looping functionality - giving them more of the ease of use associated with Visual Basic or Ruby. Also enhancing the functionality of its associative array, PHP has some powerful control commands with  list() ,  each()  and  foreach() .          Finally, PHP takes full advantage of its mixed type capability for variables and functions. Thus functions like  fopen()  return a value (a file handle in the case of  fopen()  finding a file for opening) or the Boolean  false  if no file was found. Developers have to be alert to these different Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement  - use this statement to execute some code only if a specified condit