PHP – Foreach loop

PHP – Foreach loop

 

The PHP foreach loop is native function that loops a piece of code for each key/value of an array until it reaches the last array element. It only works on an array, so do not try this with expressions, or any other type of values of a variable other than arrays.

Here is the syntax of a foreach loop

PHP

foreach ($array as $value) {
    # run this block of code;
}

The main difference of a foreach statement vs the while or for Loops is that: while and for, will continue until some condition fails. On the other hand, foreach loop will continue until it has gone through every item of an array.

For every loop, the value of the current array key is assigned to $value and the array pointer is internally moved to the next key until the last key is reached.

Here is a simple example that illustrates how the foreach statement works:

PHP

<?php 
$role = array("admin", "moderator", "publisher", "editor", "user"); 

foreach ($role as $value) {
    echo "Current role is: $value <br />";
}
?>

This will output:

PHP

Current role is: admin 
Current role is: moderator 
Current role is: publisher 
Current role is: editor 
Current role is: user

Get array key in foreach statement

The PHP foreach statement allows you to also recover and use the current can not only the value of it. Let’s look at the current example.

PHP

<?php 
$role = array("admin", "moderator", "publisher", "editor", "user"); 

foreach ($role as $key=>$value) {
    echo "Current role ID is $key, and his value is $value <br />";
}
?>

This will output:

PHP

Current role key is 0, and his value is admin 
Current role key is 1, and his value is moderator 
Current role key is 2, and his value is publisher 
Current role key is 3, and his value is editor 
Current role key is 4, and his value is user

This is actually very useful in everyday programming since allows you to build and loop throw multidimensional arrays and read every piece of information. But we will not get into more details for now. We ‘ll talk more about foreach in the arrays tutorial.

How to correctly use foreach with arrays

Since you will always use foreach with array type Values, it is a good practice to check that the value of the variable is really an array. Otherwise, you will get an error.

PHP

<?php 
$role = "anonymous"; 

foreach ($role as $value) {
    echo "Current role is: $value <br />";
}
?>

This will trigger the following error warning:

PHP

Warning: Invalid argument supplied for foreach() in C:pathtofile.php on line xx

So to prevent this we can first check if the value of our $role value is actually an array we check it like this:

PHP Coding:

<?php 
$role = "anonymous"; 

if(is_array($role)) {
	foreach ($role as $value) {
	    echo "Current role is: $value <br />";
	}
} else {
	# run a different piece of code if $role is not an array
}
?>

PHP foreach alternative syntax

The PHP foreach statements has an alternative syntax for you to use. Take a look at the next example. We are using the colon punctuation mark and the endforeach to end the syntax.

PHP

<?php 
foreach ( $array as $value):
	# do some awesome stuff here and repeat it 10 times
endforeach;
?>

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