illustrate
1. foreach is a tool that passes objects and can be used alone or in conjunction with iterators and generators.
2. If foreach is defined as a class, all attributes can be output. If it is called as a function outside the category, only public attributes can be output.
Example
classMyClass { public $var1 = 'value 1'; protected $protected = 'protected var'; private $private = 'private var'; function iterateVisible() { foreach($this as $key => $value) { //Inside the class, foreach is defined in the member method; print "$key => $valuen"; } } } $class = new MyClass(); foreach($class as $key => $value) { //Read the object as an external function print "$key => $valuen"; } //var1 => value 1, only output public properties $class->iterateVisible() //var1 => value 1, protected => protected var, private => private var output all object properties;
The above is the use of foreach in php, I hope it will be helpful to everyone.