Hey everybody I want to let you know that I have undertaken the grueling task of getting the heck away from WordPress. I was so sick of the problems and updates I had to do all the time. I am now using my ezbloo system and I am integrating all my old posts into the new system. It sucks, but in the end, I will save bundles of time. I needed to keep things simple and that is why I created ezbloo. I'll have more on this later for you guys after I am done with the total integration of my old posts here. So if you are looking for a post and need it faster, shoot me an email and I will make it a priority. [email protected]

"Php 7 for loop" and "foreach loops" will display a block of code for a number of times. We initialize the for loop with a parameter unlike what we did in the while loops. Next, we set the comparison operator and finally we increment the number of times the loop should run. Here are the files for this lesson. 


Php 7 For Loop

Here is an example of a Php 7 for loop. What we want to do it repeat the numbers until it reaches the number 5 and then stop. Set the parameters to start out at zero then set our comparison operator to see if the number is less than or equal to 5. Finally, increment the number so it adds on 1.
for($var=0; $var <= 5; $var++){
echo "<p>$var </p>";
}
In the last lesson we showed examples on how this can be done with the "while loop" and "do while loop".

Php 7 Foreach Loop

The Php 7 foreach loop is reserved for arrays and objects. First set the array then begin the foreach loop. In the parameters for the foreach loop you need to change the name of the variable. Use the "as" in the statement Here is an example of a foreach loop.
$var=array("pizza", "ice cream","salad","hamburgers");

foreach($var as $newvar){
echo "<p> I ate $newvar</p>";
}
Next we will discuss foreach loops in an object.

Php 7 For Each Loop In An Object

Use the Php 7 foreach loop in an object like this. First set the class then define the constants. Next, create your method declaration then your object. Finally, create your foreach loop to echo out your object.
//SET THE CLASS
class Food
{
//DEFINE THE CONSTANTS
public $breakfast;
public $lunch;
public $supper;

//METHOD DECLARATION
public function __construct($breakfast, $lunch, $supper)
{
$this->breakfast = $breakfast;
$this->lunch = $lunch;
$this->supper = $supper;
}
}

//CREATE THE OBJECT
$meals = new Food('eggs', 'shake', 'meatloaf');

//CREATE FOREACH LOOP
foreach ($meals as $food => $value) {
echo "<p> $food : $value </p>";
}

Conclusion on For Loops and Foreach Loops

The for loops and foreach loops will com in handy in your programming toolbox. There are several different applications where you will take advantage of the for loops in Php 7. We will be using them in our project soon. I need to teach you more about arrays, super globals and form handling before we go on with our advanced programming series. More interesting programming stuff coming up in the next tutorial. Be sure to subscribe so you do not miss a lesson.