UNIT II---FLOW CONTROL FUNCTION IN PHP



FLOW CONTROL FUNCTION IN PHP
PHP supports a number of traditional programming constructs for controlling the flow of execution of a program.
žExample
  if ($user_validated)
 echo “Welcome!”;
Else
Echo “Access Forbidden “;

SWITCHING FLOW 
žMost scripts evaluate conditions and change their behavior accordingly.


žThe capability to make decisions makes your PHPpages dynamic, able to change their output according to circumstances.

Example
 <?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!";
}
?>

                      LOOPS
žOften when you write code, you want the same block of code to run over and over again in a row.
žwhile 
   loops through a block of code as long as the specified condition is true
ždo...while - loops
  through a block of code once, and then repeats the loop as long as the specified condition is true
žfor - loops
  through a block of code a specified number of times
žforeach - loops
  through a block of code for each element in an array
  WHILE LOOP
ž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
<?php
$x = 1;

while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
?>
  DO..WHILE LOOP
ž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
<?php
$x = 1;

do {
    echo "The number is: $x <br>";
    $x++;
  while ($x <= 5);
?>
  FOR LOOP
ž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;
}
Example
<?php
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
?>
FOREACH LOOP 
ž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
<?php
$colors = array("red""green""blue""yellow");

foreach ($colors as $value) {
    echo "$value <br>";
}
?>
CODE AND BLOCKS AND BROWSER OUTPUT
ž"Installing and Configuring PHP,"you learned that you can slip in and out of HTML mode at will, using the PHP start and end tags.
ž you have discovered that you can present distinct output to the user according to a decision-making process that we can control with if and switch statements.
žImagine a script that outputs a table of values only when a variable is set to the Boolean value true..
EXAMPLE 
 A Code Block Containing Multiple print()Statements
 <html>
  <head>
 <title>Listing 5.13</title>
</head>
<body>  
<?php 7:
display_prices = true;
 if ( $display_prices ) {
 print "<table border=\"1\">";
print "<tr><td colspan=\"3\">";
 print "today's prices in dollars";
print "</td></tr>"; 1
"<tr><td>14</td><td>32</td><td>71</td></tr>";
print "</table>";
}
 ?>
 </body> 
18:
 </html>


Comments