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.
A1WEBSITEPRO Social Media Pages
Here are my social media pages, lets hook up!