To create a for loop in php, follow these simple steps:
- 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 following is pseudo php code for a for loop:
1 2 3 |
for(init counter; conditional statement; increment counter){ inner code; } |
As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to understand.
Here is an example of a php for loop:
1 2 3 4 5 6 7 8 9 10 11 |
$price = 5; echo "\"0\">";echo "";echo "";for( $count = 0; $count <= 10; $count++){ echo ""; echo "";}echo " |
The code above will produce the following output:
| Quantity | Total Price |
| 0 | 0 |
| 1 | 5 |
| 2 | 10 |
| 3 | 15 |
| 4 | 20 |
| 5 | 25 |
| 6 | 30 |
| 7 | 35 |
| 8 | 40 |
| 9 | 45 |
| 10 | 50 |




