1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 5.00 out of 5)

Php 7 For Loop Foreach Loop Simple Explanations

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.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂

More interesting programming stuff coming up in the next tutorial. Be sure to subscribe so you do not miss a lesson.

 

Subscribe To My YouTube Channel:
Php 7 For Loop Foreach Loop Simple Explanations was last modified: March 9th, 2019 by Maximus Mccullough

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share:
Php 7 For Loop Foreach Loop Simple Explanations

Pages:Previous 1 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.