Looping StatementLooping Statement
Chapter 5
MOHAMAD RAHIMI MOHAMAD
ROSMAN
IntroductionIntroduction
ļ‚—Looping statements in PHP are used to execute
the same block of code a specified number of
times.
ā—¦ While
ļ‚– loops through a block of code if and as long as a specified
condition is true
ā—¦ do...while
ļ‚– loops through a block of code once, and then repeats the
loop as long as a special 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
MOHAMAD RAHIMI MOHAMAD
ROSMAN
The while StatementThe while Statement
ļ‚—The while statement will execute a block
of code if and as long as a condition is
true.
while (condition)
code to be executed;
MOHAMAD RAHIMI MOHAMAD
ROSMAN
ExampleExample
MOHAMAD RAHIMI MOHAMAD
ROSMAN
The do...while StatementThe do...while Statement
ļ‚—The do...while statement will execute a block of
code at least once - it then will repeat the loop as
long as a condition is true.
ļ‚—Syntax
do
{
code to be executed;
}
while (condition);
MOHAMAD RAHIMI MOHAMAD
ROSMAN
ExampleExample
ļ‚— The following example will increment the value of i at
least once, and it will continue incrementing the
variable i as long as it has a value of less than 5:
MOHAMAD RAHIMI MOHAMAD
ROSMAN
The for StatementThe for Statement
ļ‚—The for statement is used when you know
how many times you want to execute a
statement or a list of statements.
ļ‚—Syntax
for (initialization; condition; increment)
{
code to be executed;
}
MOHAMAD RAHIMI MOHAMAD
ROSMAN
The for StatementThe for Statement
ļ‚—The for statement has three parameters.
ļ‚—The first parameter initializes variables
ļ‚—The second parameter holds the condition
ļ‚—The third parameter contains the increments
required to implement the loop
ļ‚—If more than one variable is included in the
initialization or the increment parameter, they
should be separated by commas.
ļ‚—The condition must evaluate to true or false.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
ExampleExample
ļ‚—What is the result of the following code?
MOHAMAD RAHIMI MOHAMAD
ROSMAN
The foreach StatementThe foreach Statement
ļ‚—The foreach statement is used to loop
through arrays.
ļ‚—For every loop, the value of the current
array element is assigned to $value (and
the array pointer is moved by one) - so on
the next loop, you'll be looking at the next
element.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
ļ‚—Syntax
foreach (array as value)
{
code to be executed;
}
MOHAMAD RAHIMI MOHAMAD
ROSMAN
ExampleExample
<html> <body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body> </html>
MOHAMAD RAHIMI MOHAMAD
ROSMAN
What is the result of the following code?
ExerciseExercise
Looping
MOHAMAD RAHIMI MOHAMAD
ROSMAN
<?php
$id=array("2001","2002","2003","2004");
$name=array("Rahul","Yunos","Mubin","Anuar");
$class=array("3A","3B","3C","3D");
$cgpa=array("3.56","3.49","3.85","3.63");
echo "Last week, a student named ".$name[0].", with UiTM ID ".$id[0].", from class ".
$class[0]." with a CGPA of ".$cgpa[0]." has been choosen to represent our
university for a debate tournament";
echo "<br>";echo "<br>";
echo "Last week, a student named ".$name[1].", with UiTM ID ".$id[1].", from class ".
$class[1]." with a CGPA of ".$cgpa[1]." has been choosen to represent our
university for a debate tournament";
echo "<br>";echo "<br>";
echo "Last week, a student named ".$name[2].", with UiTM ID ".$id[2].", from class ".
$class[2]." with a CGPA of ".$cgpa[2]." has been choosen to represent our
university for a debate tournament";
echo "<br>";echo "<br>";
echo "Last week, a student named ".$name[3].", with UiTM ID ".$id[3].", from class ".
$class[3]." with a CGPA of ".$cgpa[3]." has been choosen to represent our
university for a debate tournament";
?>
Changes the following codes by using the looping functions