Chapter 4
Chapter 4
CHAPTER 4
DECISIONS AND LOOPS
1
Outlines
Controlling the Flow of the Script
Conditional Statements
Loop Statements
2
Controlling the Flow of the Script
Describes how to change the order in which simple statements are executed by using complex
statements such as conditional statements or loops.
The PHP conditional statements are
1. if statement
2. switch statement.
The PHP loop statements are
1. for loops
2. while loops
3. do….while loops.
3
Using comparison operators
PHP offers several comparison operators that you can use to compare values.
Comparison Operators: ==, ===,!=,!==, >, <, >=, <=
Logical Operators: &&, ||, !
4
Using Conditional Statements
A conditional statement execute a block of statements only when certain conditions are true.
Here are two useful types of conditional statements: if statement and switch statement.
Using if statements
1) if ( condition )
{ code to be executed if condition is true; }
2) if ( condition )
{ code to be executed if condition is true; }
else { code to be executed if condition is false; }
5
Using Conditional Statements
Using if statements
3) if ( condition )
{ code to be executed if condition is true; }
elseif ( condition )
{ code to be executed elseif condition is true; }
else
{ code to be executed elseif condition is false; }
6
Using Conditional Statements(Example)
<html>
Output:
<body>
We have exactly 23 widgets in stock!
<?php
$widgets = 23;
if($widgets == 23){
echo "We have exactly 23 widgets in stock!";
}
?>
</body>
</html>
7
Using Conditional Statements(Example)
<html>
<body>
<?php
$widgets = 23;
if($widgets >= 10&&$widgets <= 20)
{
echo "We have between 10 and 20 widgets in stock.";
}
?>
</body>
</html>
8
Conditionals: if else(Example)
<html><body>
<?php Output:
$widgets=5; Less than 10 widgets left. Time to order some more!
if($widgets >= 10)
{
echo "We have plenty of widgets in stock.";
}
else
{
echo "Less than 10 widgets left. Time to order some more!";
}
?>
</body></html>
9
Conditionals: if ….elseif…else (Example)
<html>
<body> Output:
<?php Less than 10 widgets left. Time to order some more!
$widgets=5;
if($widgets >= 10){
echo "We have plenty of widgets in stock.";
} else if($widgets >= 5){
echo "Less than 10 widgets left. Time to order some more!";
} else {
echo "Panic stations: Less than 5 widgets left! Order more now!";
}
?>
</body>
</html>
10
Conditional Switch Statement
The switch statement tests the value of one variable and executes the block of statements for the
matching value of the variable. The general format is as follows:
switch ( $variablename )
{
case value : block of statements; break;
case value : block of statements; break;
...
default: block of statements;
} //end of switch
11
Conditional Switch Statement
<html>
<body> Output:
<?php Your favorite color is red!
$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, or green!";
}
?>
</body>
</html>
12
Loop Statement
for loops: Sets up a counter; repeats a block of statements until the counter reaches a specified
number.
block of statements;
13
Looping: for
Can loop depending on a "counter"
$i=1; $i<=5; $i++ output
<?php Output:
i=1 ; 1<=5; 1++ Hello
for ($i=1; $i<=5; $i++) World!
Hello World!
{ Hello World! i=2; 2<=5; 2++ Hello World!
Hello World!
echo "Hello World!<br />"; Hello World! i=3; 3<=5; 3++ Hello
Hello World! World!
}
i=4; 4<=5; 4++ Hello
?>
World!
i=5; 5<=5; 5++ Hello
World!
14
Looping: for
Test the loop counter is even or odd Output
The number is 0 and it's even.
<?php The number is 1 and it's odd.
for($i=0; $i<10; $i++){ The number is 2 and it's even.
The number is 3 and it's odd.
if($i%2==0)
The number is 4 and it's even.
echo "<p>The number is $i and it’s even.</p>"; The number is 5 and it's odd.
The number is 6 and it's even.
else
The number is 7 and it's odd.
echo "<p>The number is $i and it’s odd.</p>"; The number is 8 and it's even.
} The number is 9 and it's odd.
?>
15
Looping: for
Nesting for loops
Multiply by 1
You can nest for loops inside of for loops. Suppose you want to 1x1=1
print out the times tables from 1 to 9. You can use the following 1x2=2
statements: ...
1x8=8
<?php
for($i=1;$i<=9;$i++) 1x9=9
{ Multiply by 2
echo "Multiply by $i <br/>"; 2x1=2
for($j=1;$j<=9;$j++) 2x2=4
{
$result = $i * $j; ...
echo "$i x $j = $result<br/>"; 2 x 8 = 16
}//end of the inner loop 2 x 9 = 18
}//end of the outer loop ……
?>
16
Looping: while
Using while loops
A while loop continues repeating as long as certain conditions are true. The loop works as
follows:
1. You set up a condition.
2. The condition is tested at the top of each loop.
3. If the condition is true, the loop repeats. If the condition is not true, the loop stops.
The following is the general format of a while loop:
while ( condition )
{
block of statements
}
17
Looping: while
<?php Output:
The number is 0
$i=0; The number is 1
while($i<10) The number is 2
The number is 3
{ The number is 4
The number is 5
echo '<p>The number is '.$i .'</p>'; The number is 6
$i=$i+1; //$i++ The number is 7
The number is 8
} The number is 9
?>
18
Looping: while
<html><head></head>
Output
<body>
<?php
The number is 1
$i=1;
The number is 2
while($i <= 5)
The number is 3
{
The number is 4
echo "The number is $i <br/>";
The number is 5
$i++;
}
?>
</body>
</html>
19
Looping: do…..while
Using do..while loops
do{
block of statements
} while(condition);
20
Looping: do…..while
<html><head></head> Output:
<body> The number is 1
<?php The number is 2
$i=0; The number is 3
do{ The number is 4
The number is 5
$i++;
The number is 6
echo "The number is $i <br/>";
The number is 7
}while($i <= 10); The number is 8
?> The number is 9
</body> The number is 10
</html> The number is 11
21
Looping: do…..while
Output:
<html><head></head> The number is 101
<body>
<?php
$i=100;
do{
$i++;
echo "The number is $i <br/>";
}while($i <= 10);
?>
</body>
</html>
22
Thank You
23