<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>For Each</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="com.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="com.examples.arrays.html">Arrays and Array-style COM properties</a></div> <div class="up"><a href="com.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="com.examples.foreach" class="section"> <h2 class="title">For Each</h2> <p class="para"> Starting with PHP 5, you may use PHP's own <a href="control-structures.foreach.html" class="link">foreach</a> statement to iterate over the contents of a standard COM/OLE IEnumVariant. In layman's terms, this means that you can use foreach in places where you would have used <em>For Each</em> in VB/ASP code. </p> <p class="para"> <div class="example" id="example-5350"> <p><strong>Example #1 For Each in ASP</strong></p> <div class="example-contents"> <div class="aspcode"><pre class="aspcode"><% Set domainObject = GetObject("WinNT://Domain") For Each obj in domainObject Response.Write obj.Name & "<br />" Next %></pre> </div> </div> </div> </p> <p class="para"> <div class="example" id="example-5351"> <p><strong>Example #2 while() ... Next() in PHP 4</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php <br />$domainObject </span><span style="color: #007700">= new </span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"WinNT://Domain"</span><span style="color: #007700">); <br />while (</span><span style="color: #0000BB">$obj </span><span style="color: #007700">= </span><span style="color: #0000BB">$domainObject</span><span style="color: #007700">-></span><span style="color: #0000BB">Next</span><span style="color: #007700">()) { <br /> echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">Name </span><span style="color: #007700">. </span><span style="color: #DD0000">"<br />"</span><span style="color: #007700">; <br />} <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5352"> <p><strong>Example #3 foreach in PHP 5</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php <br />$domainObject </span><span style="color: #007700">= new </span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"WinNT://Domain"</span><span style="color: #007700">); <br />foreach (</span><span style="color: #0000BB">$domainObject </span><span style="color: #007700">as </span><span style="color: #0000BB">$obj</span><span style="color: #007700">) { <br /> echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">Name </span><span style="color: #007700">. </span><span style="color: #DD0000">"<br />"</span><span style="color: #007700">; <br />} <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="com.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="com.examples.arrays.html">Arrays and Array-style COM properties</a></div> <div class="up"><a href="com.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>