PHP – For loop

PHP – 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 Coding:

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

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

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.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

 

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars