PHP - For loop

  »   PHP  »   PHP Tutorial - For loop

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">';
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.

HTML<table border="1" align="center">
   <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>

Demo

ProductQuantityDiscountTotal Price
Pencils5010%$45
Pencils10020%$80
Pencils15030%$105
Pencils20040%$120
Pencils25050%$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
}
?>

For loop vs While loop - aren't they the same

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++
}
?>

PHP for statement - alternative syntax

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.