Iterating Through Object Properties : foreach « Statement « PHP






Iterating Through Object Properties

 
<?
    class Person {
            public $FirstName = "B";
            public $MiddleName = "T";
            public $LastName = "M";
            private $Password = "asdfasdf";
            public $Age = 29;
            public $HomeTown = "Edinburgh";
            public $FavouriteColor = "Purple";
    }

    $bill = new Person( );

    foreach($bill as $var => $value) {
            echo "$var is $value\n";
    }
?>
  
  








Related examples in the same category

1.Foreach Demo
2.Looping through an Enumerated Array
3.Array element order and foreach()
4.Iterating through a multidimensional array with foreach()
5.Modifying an array with foreach()
6.Using foreach() to Iterate Through an Array
7.Using foreach() with numeric arrays
8.Displaying the contents of an array using a loop
9.Implementing the traversable Iterator