Hello, welcome to zhullyblog. If this is your first time here , please
Subscribe to Zhullyblog by Email to get latest tutorial.
PHP Loops
What are loops?
Loops are used to execute block of code repeatedly until a certain condition is met. Loops are used to execute the same code over and over until a specified condition is met.
There are different types of loops but PHP supports four different types of loop.
• While loop
•Do…while loop
• For loop
• foreach loop
The while loop
The while loop is a control loop that runs until a certain condition is met.
The while loop will continue to run until the condition evaluate to true. The while loop always evaluate to true.
SYNTAX
While(condition) {
//The body of the code
}
Let’s take a quick example
<?php
$x = 1;
while($x <= 10)
{
echo "$x ";
}
?>
The do..while loop
The do while loop evaluates the condition at the end of each loop.
This means that it runs the loop before checking the condition.
SYNTAX
do {
//Body of the code
}
While(condition);
Lets take a quick example
<?php
$x = 1;
do {
echo "$x";
} while($x <= 10)
?>
For loop
The for loop executes a statement as long as the condition stated is met. It is used to execute a statement for a specific number of times. This kind of loop is used when the user knows the number of times the loop will run.
SYNTAX
For(initialization ; condition ; increment ) {
//body of the code
}
Alright let’s take a good example
<?php
for($x = 1; $x <= 10; $x++)
{
echo "$x <br/>";
}
?>
Comments
Post a Comment