PHP loops:
- for
- and while.
- 1-while loop
- 2-do ... while loop
- 3-for loop
- 4-foreach loop
In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements.
PHP has two major groups of looping statements:
PHP has two major groups of looping statements:
for :
The For statements are best used when you want to perform a loop a specific number of times.while :
The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.PHP loops are control structures and you can use them the execute a code block more times. It means you don't have to copy and paste your code many times in the file just use a right loop statement.
In php we use four types loos those are describe below:
PHP for Loop:
- Set a counter variable to some initial value.
- Check to see if the conditional statement is true.
- Execute the code within the loop.
- Increment a counter at the end of each iteration through the loop.
- The 1st parameter sets a counter
- 2nd parameter defines how high the counter should get before breaking from the loop
- 3rd parameter is used to increment the counter.
- Deleting spam email
- sealing 50 envelopes
PHP for loops allow you to execute the same piece of code for a specified number of times. Once the specified number has been reached, the program will break out of the loop and continue processing the rest of the page.
The common tasks that are covered by a for loop are:
Syntax
A PHP for loop requires 3 parameters.
for (initialization;condition; increment/decreament)
{
Code to be executed while the counter is less than the specified count;
}
{
Code to be executed while the counter is less than the specified count;
}
Example
<?php for ($xint=0; $xint<=5; $xint++)
{
echo $xint;
}
?>Output
012345Example
<?php{
?>Output
| No of items | Price |
|---|---|
| 10 | 50 |
| 20 | 100 |
| 30 | 150 |
| 40 | 200 |
| 50 | 250 |
| 60 | 300 |
| 70 | 350 |
| 80 | 400 |
| 90 | 450 |
| 100 | 500 |
PHP while Loop:
PHP while loops allow you to execute the same piece of code continuously while a certain condition is true. Once the condition becomes false, the program will break out of the loop and continue processing the rest of the page.
Repetitive tasks are always a difficult to us as
and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.
Syntax
initialization;
for (condition)
{
Code to be executed while
the counter is less than the specified count;
increment/decreament;
}
Example
<?php
$i=1;
while ($i<=9)
{
echo " $i ";
$i++;
}
?>
Output
123456789
Example
<?php
$price = 5;
$item = 10;
echo "<table border='1' align='center'>";
echo "<tr><th>No of items</th>";
echo "<th>Price</th></tr>";
while ( $item <= 100 )
{
echo "<tr><td>";
echo $item;
echo "</td><td>";
echo $price * $item;
echo "</td></tr>";
$item = $item + 10;
}
echo "</table>";
?>
Output
No of items Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500
PHP while Loop:
PHP while loops allow you to execute the same piece of code continuously while a certain condition is true. Once the condition becomes false, the program will break out of the loop and continue processing the rest of the page.
Repetitive tasks are always a difficult to us as
and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.
Syntax
the counter is less than the specified count;
Example
<?php?>Output
123456789Example
<?php?>Output
| No of items | Price |
|---|---|
| 10 | 50 |
| 20 | 100 |
| 30 | 150 |
| 40 | 200 |
| 50 | 250 |
| 60 | 300 |
| 70 | 350 |
| 80 | 400 |
| 90 | 450 |
| 100 | 500 |
PHP do-while Loop:
The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false.
On the other hand, a do-while loop always executes its block of code at least once. This is because the conditional statement is not checked until after the contained code has been executed.
Syntax
Example
<?php?>
No comments:
Post a Comment