If you want to execute a loop for a given number of times, the for statement is what you are looking for.
Here is a simplified example of the for syntax
for (start counter; check current counter value; increment counter) { # run this code }Although this is a simplified form of the for statement it may be easier to understand with a practical example
Imagine you sell pencils in your store. All pencils cost the same, but when someone buys lots of them, you offer a discount. Here is how you can dynamically calculate and display it to your customers.
php<?php
echo '<table border="1" align="center" width="100%">';
echo '<tr>';
echo'<th>Product</th>';
echo'<th align="center">Quantity</th>';
echo'<th align="center">Discount</th>';
echo '<th align="center">Total Price</th>';
echo "</tr>";
$product = "Pencils";
$q = 50; // quantity
$d = 10; //
for ( $counter = 1; $counter <= 5; $counter++) {
echo '<tr>';
echo '<td>'.$product.'</td>';
echo '<td align="center">'.($counter * $q).'</td>';
echo '<td align="center">'.($counter * $d).'%</td>';
echo '<td align="center">$'.($counter * $q - $counter * $q * $counter * $d / 100).'</td>';
echo '</tr>';
}
echo '</table>';
?>
This is how your generated HTML code will look like. We have formatted the code for easier reading.
php<table border="1" align="center" width="100%">
<tr>
<th>Product</th>
<th align="center">Quantity</th>
<th align="center">Discount</th>
<th align="center">Total Price</th>
</tr>
<tr>
<td>Pencils</td>
<td align="center">50</td>
<td align="center">10%</td>
<td align="center">$45</td>
</tr>
<tr>
<td>Pencils</td>
<td align="center">100</td>
<td align="center">20%</td>
<td align="center">$80</td>
</tr>
<tr>
<td>Pencils</td>
<td align="center">150</td>
<td align="center">30%</td>
<td align="center">$105</td>
</tr>
<tr>
<td>Pencils</td>
<td align="center">200</td>
<td align="center">40%</td>
<td align="center">$120</td>
</tr>
<tr>
<td>Pencils</td>
<td align="center">250</td>
<td align="center">50%</td>
<td align="center">$125</td>
</tr>
</table>
Product | Quantity | Discount | Total Price |
---|---|---|---|
Pencils | 50 | 10% | $45 |
Pencils | 100 | 20% | $80 |
Pencils | 150 | 30% | $105 |
Pencils | 200 | 40% | $120 |
Pencils | 250 | 50% | $125 |
Ok, maybe I have gone too far and added too much to illustrate, but I hope you get the point. Here is the for statement in his simpler form.
php<?php
for ( $i=1; $i <= 10; $i++) {
# do some awesome stuff here and repeat it 10 times
}
?>
Well, you can actually use both for and while loops to achieve the same result. But in some cases, the for syntax it's more compact and easier to read. So take that into consideration when you choose one or the other
php<?php
$i=1;
while ( $i <= 10; ) {
# do some awesome stuff here and repeat it 10 times
$i++
}
?>
You are right! The for statement has his own alternative syntax. Here is an alternative way to write a for loop in PHP.
php<?php
for ( $i=1; $i <= 10; $i++):
# do some awesome stuff here and repeat it 10 times
endfor;
?>
Simple as that...see you in the foreach loop tutorial on the next page.