Course Learning
PHP Looping Statements
Conditional Looping Statement
Loops are used to execute the same block of code
again and again, as long as a certain condition is met.
The basic idea behind a loop is to automate the
repetitive tasks within a program to save the time
and effort.
while Loop
loops through a block of code as long as the
condition specified evaluates to true.
Syntax Php Script Output
while (expression) <?php
{ $num = 1;
code to execute; while ($num <= 10)
} {
print "Number is $num<br />\n";
$num++;
}
print 'Done.’;
?>
do while Loop
The block of code executed once and then condition is evaluated. If the
condition is true the statement is repeated as long as the specified
condition is true.
Syntax Php Script Output
do <?php
{ $num = 1;
code to execute; do {
} echo "Number is ".$num."<br />";
while(expression); $num++;
} while ($num <= 10);
echo "Done.";
?>
for Loop
loops through a block of code until the counter reaches a
specified number.
Syntax Php Script Output
for(initialization; condition; increment) <?php
{ for ($num = 1; $num <= 10; $num++)
// Code to be executed {
} print "Number is $num<br />\n";
}
?>
for each loop
loops through a block of code for each element in an array.
Syntax Php Script Output
foreach ($array as $value) <?php
{ $cars = array("Toyota", "Honda", "Nissan", "Ford");
code to be executed;
} foreach ($cars as $value)
{
echo "$value <br>";
}
?>
End of Presentation