PHP Loop Statements
A loop in PHP(as well in other languages) performs a block/chunk of code a specified number of times that can be used to save time when you want a piece of code to be executed at different times but with the same function. Loop statements are:
while – performs the block/chunk of code as long as the set condition is satisfied
do while – runs through a block of code once over then checks a parameter if the condition results true
for – executes code for the specified number of times
foreach – goes though a block of code for each and every element of an array
while – loop
syntax : while(condition)
do this
Sample code: (The code below will continue to execute as long as the condition (variable i) is less than or equal to 5 with variable i increasing by an increment of 1 each time the loop executes)
while($i<=5)
{
echo "The number is " . $i . "
“;
$i++;
}
?>
The next posts will discuss the other loop statements.
Filed under: Basic Programming, Sample Code by Avatar
