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!";
}
?>
$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!";
}
?>
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;
}
code to be executed;
}
EXAMPLE
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
$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);
code to be executed;
} while (condition is true);
EXAMPLE
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
}
$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;
}
code to be executed;
}
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
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;
}
code to be executed;
}
EXAMPLE
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
$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
Post a Comment