loop iteration in php for each code example

Example 1: for each loop php

<?php 
$colors = array("red", "green", "blue", "yellow"); 

 
foreach ($colors as $value) {
  echo "$value <br>";
}
?>

Example 2: iteration in php

<?php
class MyClass
{
    public $var1 = 'value 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';

    protected $protected = 'protected var';
    private   $private   = 'private var';

    function iterateVisible() {
       echo "MyClass::iterateVisible:\n";
       foreach ($this as $key => $value) {
           print "$key => $value\n";
       }
    }
}

$class = new MyClass();

foreach($class as $key => $value) {
    print "$key => $value\n";
}
echo "\n";


$class->iterateVisible();

?>

Tags:

Php Example