<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Magic Book - Free PHP Scripts, Tutorials and Downloads &#187; Loops</title>
	<atom:link href="http://www.phpmagicbook.com/category/loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmagicbook.com</link>
	<description>PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips</description>
	<lastBuildDate>Tue, 15 Jun 2010 13:22:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP ForEach Loop</title>
		<link>http://www.phpmagicbook.com/php-for-each-loop/</link>
		<comments>http://www.phpmagicbook.com/php-for-each-loop/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 21:27:17 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[For Each Loop]]></category>
		<category><![CDATA[Loops]]></category>
		<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[for each]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=161</guid>
		<description><![CDATA[PHP provides an easy way to use every element of an array with the For each statement. For each executes the code at each item in the specified array. While a For Loop and While Loop will continue until some condition fails, the For Each loop will continue until it has gone through every item [...]<p><a href="http://www.phpmagicbook.com/php-for-each-loop/">PHP ForEach Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>PHP provides an easy way to use every element of an array with the <strong>For each</strong> statement. For each executes the code at each item in the specified array. While a For Loop and While Loop will continue until some condition fails, the For Each loop will continue until it has gone through every item in the array.</p>
<p>We have an associative array that stores the names of people in a company as the keys with the values being their age. We want to know how old everyone is at work so we use a Foreach loop to print out everyone&#8217;s name and age.</p>
<p><span id="more-231"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Imran&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;18&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Kamran&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;26&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Jawad&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;35&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Javed&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;36&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$employeeAges</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Rashid&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;32&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$employeeAges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Name: <span style="color: #006699; font-weight: bold;">$key</span>, Age: <span style="color: #006699; font-weight: bold;">$value</span> &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output:</span>
<span style="color: #666666; font-style: italic;">// Name: Imran, Age: 18</span>
<span style="color: #666666; font-style: italic;">// Name: Kamran, Age: 26</span>
<span style="color: #666666; font-style: italic;">// Name: Jawad, Age: 35</span>
<span style="color: #666666; font-style: italic;">// Name: Javed, Age: 36</span>
<span style="color: #666666; font-style: italic;">// Name: Rashid, Age: 32</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://www.phpmagicbook.com/php-for-each-loop/">PHP ForEach Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-functions/" title="PHP Functions">PHP Functions</a></li><li><a href="http://www.phpmagicbook.com/php-do-while-loop/" title="PHP Do While Loop">PHP Do While Loop</a></li><li><a href="http://www.phpmagicbook.com/php-while-loop/" title="PHP While Loop">PHP While Loop</a></li><li><a href="http://www.phpmagicbook.com/php-for-loop/" title="PHP For Loop">PHP For Loop</a></li><li><a href="http://www.phpmagicbook.com/show-a-part-of-string-substr/" title="Show A Part Of String &#8211; substr in PHP">Show A Part Of String &#8211; substr in PHP</a></li><li><a href="http://www.phpmagicbook.com/find-page-url-php/" title="Find Page URL With PHP &#8211; File Path">Find Page URL With PHP &#8211; File Path</a></li><li><a href="http://www.phpmagicbook.com/browser-check-with-php/" title="Browser Check With PHP">Browser Check With PHP</a></li><li><a href="http://www.phpmagicbook.com/php-string-case-capitalization/" title="PHP &#8211; String Case &#8211; Capitalization">PHP &#8211; String Case &#8211; Capitalization</a></li><li><a href="http://www.phpmagicbook.com/php-post-and-get/" title="PHP Post And Get">PHP Post And Get</a></li><li><a href="http://www.phpmagicbook.com/get-data-as-an-array-download-php-example/" title="Get Data As An Array &#8211; Download PHP Example">Get Data As An Array &#8211; Download PHP Example</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/php-for-each-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Do While Loop</title>
		<link>http://www.phpmagicbook.com/php-do-while-loop/</link>
		<comments>http://www.phpmagicbook.com/php-do-while-loop/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 21:14:45 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[Do Loop]]></category>
		<category><![CDATA[Do While Loop]]></category>
		<category><![CDATA[Loops]]></category>
		<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[do]]></category>
		<category><![CDATA[do while]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=160</guid>
		<description><![CDATA[A &#8220;do while&#8221; loop is a slightly modified version of the while loop. A do-while loop first do&#8217;s and secondly checks the while condition! PHP Do While Loop Structure - Define - Do - Print - Increment or decrement - Condition PHP Do While Loop Example &#60;?php $num = 1; do &#123; echo $num; $num [...]<p><a href="http://www.phpmagicbook.com/php-do-while-loop/">PHP Do While Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A &#8220;do while&#8221; loop is a slightly modified version of the <a href="http://www.phpmagicbook.com/php-while-loop/">while loop</a>. A do-while loop first do&#8217;s and secondly checks the while condition!</p>
<h4>PHP Do While Loop Structure</h4>
<p>- Define<br />
- Do<br />
- Print<br />
- Increment or decrement<br />
- Condition</p>
<p><span id="more-230"></span></p>
<h4>PHP Do While Loop Example</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">do</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$num</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output: 13579</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Displaying the numbers from 1 to 10, with their squares</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">do</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$count</span> squared is <span style="color: #006699; font-weight: bold;">$square</span> &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output:</span>
<span style="color: #666666; font-style: italic;">// 1 squared is 1</span>
<span style="color: #666666; font-style: italic;">// 2 squared is 4</span>
<span style="color: #666666; font-style: italic;">// 3 squared is 9</span>
<span style="color: #666666; font-style: italic;">// 4 squared is 16</span>
<span style="color: #666666; font-style: italic;">// 5 squared is 25</span>
<span style="color: #666666; font-style: italic;">// 6 squared is 36</span>
<span style="color: #666666; font-style: italic;">// 7 squared is 49</span>
<span style="color: #666666; font-style: italic;">// 8 squared is 64</span>
<span style="color: #666666; font-style: italic;">// 9 squared is 81</span>
<span style="color: #666666; font-style: italic;">// 10 squared is 100</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://www.phpmagicbook.com/php-do-while-loop/">PHP Do While Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-for-each-loop/" title="PHP ForEach Loop">PHP ForEach Loop</a></li><li><a href="http://www.phpmagicbook.com/php-while-loop/" title="PHP While Loop">PHP While Loop</a></li><li><a href="http://www.phpmagicbook.com/php-for-loop/" title="PHP For Loop">PHP For Loop</a></li><li><a href="http://www.phpmagicbook.com/find-page-url-php/" title="Find Page URL With PHP &#8211; File Path">Find Page URL With PHP &#8211; File Path</a></li><li><a href="http://www.phpmagicbook.com/php-string-case-capitalization/" title="PHP &#8211; String Case &#8211; Capitalization">PHP &#8211; String Case &#8211; Capitalization</a></li><li><a href="http://www.phpmagicbook.com/php-post-and-get/" title="PHP Post And Get">PHP Post And Get</a></li><li><a href="http://www.phpmagicbook.com/get-data-as-an-array-download-php-example/" title="Get Data As An Array &#8211; Download PHP Example">Get Data As An Array &#8211; Download PHP Example</a></li><li><a href="http://www.phpmagicbook.com/php-arrays/" title="PHP Arrays">PHP Arrays</a></li><li><a href="http://www.phpmagicbook.com/php-strings/" title="PHP Strings">PHP Strings</a></li><li><a href="http://www.phpmagicbook.com/local-and-glob-scope/" title="Local And Global Variable Scope in PHP">Local And Global Variable Scope in PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/php-do-while-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP While Loop</title>
		<link>http://www.phpmagicbook.com/php-while-loop/</link>
		<comments>http://www.phpmagicbook.com/php-while-loop/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 21:07:11 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[Loops]]></category>
		<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[While Loop]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=159</guid>
		<description><![CDATA[The while keyword takes a condition in parentheses, and the code block that follows is repeated while that condition is true. If the condition is false initially, the code block will not be repeated at all. The repeating code must perform some action that affects the condition in such a way that the loop condition [...]<p><a href="http://www.phpmagicbook.com/php-while-loop/">PHP While Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The while keyword takes a condition in parentheses, and the code block that follows is repeated while that condition is true. If the condition is false initially, the code block will not be repeated at all.</p>
<p>The repeating code must perform some action that affects the condition in such a way that the loop condition will eventually no longer be met; otherwise, the loop will repeat forever. Such loops are Infinite Loops.</p>
<p><span id="more-229"></span></p>
<h4>Structure Of PHP While Loop</h4>
<p>- Define<br />
- Condition<br />
- Print<br />
- Increment or decrement</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">while ( conditional statement is true)
{
	//do the task;
}</pre></div></div>

<h4>PHP While Loop Example</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$num</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output: 3579</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$count</span> squared is <span style="color: #006699; font-weight: bold;">$square</span> &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output:</span>
<span style="color: #666666; font-style: italic;">// 1 squared is 1</span>
<span style="color: #666666; font-style: italic;">// 2 squared is 4</span>
<span style="color: #666666; font-style: italic;">// 3 squared is 9</span>
<span style="color: #666666; font-style: italic;">// 4 squared is 16</span>
<span style="color: #666666; font-style: italic;">// 5 squared is 25</span>
<span style="color: #666666; font-style: italic;">// 6 squared is 36</span>
<span style="color: #666666; font-style: italic;">// 7 squared is 49</span>
<span style="color: #666666; font-style: italic;">// 8 squared is 64</span>
<span style="color: #666666; font-style: italic;">// 9 squared is 81</span>
<span style="color: #666666; font-style: italic;">// 10 squared is 100</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://www.phpmagicbook.com/php-while-loop/">PHP While Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-for-each-loop/" title="PHP ForEach Loop">PHP ForEach Loop</a></li><li><a href="http://www.phpmagicbook.com/php-do-while-loop/" title="PHP Do While Loop">PHP Do While Loop</a></li><li><a href="http://www.phpmagicbook.com/php-for-loop/" title="PHP For Loop">PHP For Loop</a></li><li><a href="http://www.phpmagicbook.com/find-page-url-php/" title="Find Page URL With PHP &#8211; File Path">Find Page URL With PHP &#8211; File Path</a></li><li><a href="http://www.phpmagicbook.com/php-string-case-capitalization/" title="PHP &#8211; String Case &#8211; Capitalization">PHP &#8211; String Case &#8211; Capitalization</a></li><li><a href="http://www.phpmagicbook.com/php-post-and-get/" title="PHP Post And Get">PHP Post And Get</a></li><li><a href="http://www.phpmagicbook.com/get-data-as-an-array-download-php-example/" title="Get Data As An Array &#8211; Download PHP Example">Get Data As An Array &#8211; Download PHP Example</a></li><li><a href="http://www.phpmagicbook.com/php-arrays/" title="PHP Arrays">PHP Arrays</a></li><li><a href="http://www.phpmagicbook.com/php-strings/" title="PHP Strings">PHP Strings</a></li><li><a href="http://www.phpmagicbook.com/local-and-glob-scope/" title="Local And Global Variable Scope in PHP">Local And Global Variable Scope in PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/php-while-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP For Loop</title>
		<link>http://www.phpmagicbook.com/php-for-loop/</link>
		<comments>http://www.phpmagicbook.com/php-for-loop/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 19:46:11 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[For Loop]]></category>
		<category><![CDATA[Loops]]></category>
		<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[for]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=158</guid>
		<description><![CDATA[For loop consists of three phases. i.e. Define Condition Increment or decrement First we define something. Then we apply a condition and then under that condition we increment or decrement that defined thing. In complex way; - The first part is an expression that is evaluated once when the loop begins. - The second part [...]<p><a href="http://www.phpmagicbook.com/php-for-loop/">PHP For Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>For loop consists of three phases. i.e.</p>
<ul>
<li>Define</li>
<li>Condition</li>
<li>Increment or decrement</li>
</ul>
<p>First we define something. Then we apply a condition and then under that condition we increment or decrement that defined thing.</p>
<p>In complex way;<br />
- The first part is an expression that is evaluated once when the loop begins.<br />
- The second part is the condition. While the condition is true, the loop continues repeating. If the condition is false to start with, the following code block is not executed at all.<br />
- The third part is an expression that is evaluated once at the end of each pass of the loop.</p>
<p><span id="more-228"></span></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">for ( initialize a counter;
       conditional statement;
       increment a counter)
{
	do this code;
}</pre></div></div>

<h4>Examples</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 12345678910</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Explanation of above example:</span>
<span style="color: #666666; font-style: italic;">// In this we define a var i and give it a value of 1.</span>
<span style="color: #666666; font-style: italic;">// We tell the script to increment the value of i and print it.</span>
<span style="color: #666666; font-style: italic;">// And keep incrementing it and printing it until it reaches to the value of 10.</span>
<span style="color: #666666; font-style: italic;">// Or keep incrementing it and printing it if it is less then 10 or equal to 10.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Caution:</span>
<span style="color: #666666; font-style: italic;">// Now do not run a loop without a condition.</span>
<span style="color: #666666; font-style: italic;">// Because it can go pretty mad without a condition.</span>
<span style="color: #666666; font-style: italic;">// and go in cyclic chain process which will never end.</span>
<span style="color: #666666; font-style: italic;">// and your PC or at least your PHP host will be held.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Other Examples Of For Loop In PHP</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">print</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 12345678910</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> <span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 12345678910</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 12345678910</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$num</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$num</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 1 2 3 4 6 7 8 9</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$num</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$num</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output of this script will be. 1 2 3 4</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Using PHP for loop allows you to use much less code to do the same thing as with while and do.</p>
<p><a href="http://www.phpmagicbook.com/php-for-loop/">PHP For Loop</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/get-data-as-an-array-download-php-example/" title="Get Data As An Array &#8211; Download PHP Example">Get Data As An Array &#8211; Download PHP Example</a></li><li><a href="http://www.phpmagicbook.com/php-for-each-loop/" title="PHP ForEach Loop">PHP ForEach Loop</a></li><li><a href="http://www.phpmagicbook.com/php-do-while-loop/" title="PHP Do While Loop">PHP Do While Loop</a></li><li><a href="http://www.phpmagicbook.com/php-while-loop/" title="PHP While Loop">PHP While Loop</a></li><li><a href="http://www.phpmagicbook.com/find-page-url-php/" title="Find Page URL With PHP &#8211; File Path">Find Page URL With PHP &#8211; File Path</a></li><li><a href="http://www.phpmagicbook.com/php-string-case-capitalization/" title="PHP &#8211; String Case &#8211; Capitalization">PHP &#8211; String Case &#8211; Capitalization</a></li><li><a href="http://www.phpmagicbook.com/php-post-and-get/" title="PHP Post And Get">PHP Post And Get</a></li><li><a href="http://www.phpmagicbook.com/php-arrays/" title="PHP Arrays">PHP Arrays</a></li><li><a href="http://www.phpmagicbook.com/php-strings/" title="PHP Strings">PHP Strings</a></li><li><a href="http://www.phpmagicbook.com/local-and-glob-scope/" title="Local And Global Variable Scope in PHP">Local And Global Variable Scope in PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/php-for-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>For Loop in PHP</title>
		<link>http://www.phpmagicbook.com/for-loop/</link>
		<comments>http://www.phpmagicbook.com/for-loop/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 00:10:13 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[Loops]]></category>
		<category><![CDATA[Randomizing]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=130</guid>
		<description><![CDATA[I am describing for loop with an example that include randomizing number display using images from a directory. Use following script and create a folder nb and put images 0.gif, 1.gif, &#8230; , 9.gif containing same digit over each and see. PHP Code &#60;?php $mynumber= rand&#40;1111111111,9999999999&#41;; // echo $mynumber; $ilength=strlen&#40;$mynumber&#41;; for&#40;$i=0; $i&#60;$ilength; $i++&#41; &#123; $ipic= [...]<p><a href="http://www.phpmagicbook.com/for-loop/">For Loop in PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.phpmagicbook.com/wp-content/uploads/2008/06/random-numbers-for-loop.jpg" alt="for loop" title="For Loop" width="400" height="42" class="aligncenter size-full wp-image-131" /></p>
<p>I am describing for loop with an example that include randomizing number display using images from a directory. Use following script and create a folder nb and put images 0.gif, 1.gif, &#8230; , 9.gif containing same digit over each and see.</p>
<p><span id="more-205"></span></p>
<h4>PHP Code</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$mynumber</span><span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1111111111</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9999999999</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// echo $mynumber;</span>
<span style="color: #000088;">$ilength</span><span style="color: #339933;">=</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mynumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$ilength</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$ipic</span><span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mynumber</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sFilePath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nb/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ipic</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$sFilePath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;nb/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ipic</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.gif&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;img src=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$sFilePath</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/for-loop-rand-numbers.rar' class="download">For Loop Rand Numbers Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/for-loop/">For Loop in PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/elseif-conditions/" title="Elseif Conditions in PHP">Elseif Conditions in PHP</a></li><li><a href="http://www.phpmagicbook.com/easiest-way-ever-php-email/" title="Easiest Way Ever PHP eMail Script">Easiest Way Ever PHP eMail Script</a></li><li><a href="http://www.phpmagicbook.com/else-statement/" title="PHP Else Statement">PHP Else Statement</a></li><li><a href="http://www.phpmagicbook.com/date-difference-in-php-find-date-difference/" title="Date Difference In PHP &#8211; Find Date Difference">Date Difference In PHP &#8211; Find Date Difference</a></li><li><a href="http://www.phpmagicbook.com/optimize-blog-post-title-tag-for-seo/" title="Optimize Blog Post Title Tag For SEO">Optimize Blog Post Title Tag For SEO</a></li><li><a href="http://www.phpmagicbook.com/hide-ads-from-admin-at-blog/" title="Hide Ads from Admin at Blog">Hide Ads from Admin at Blog</a></li><li><a href="http://www.phpmagicbook.com/how-to-display-some-content-at-homepage-single-post-of-blog/" title="How To Display Some Content at Just Home Page and Single Post of Blog">How To Display Some Content at Just Home Page and Single Post of Blog</a></li><li><a href="http://www.phpmagicbook.com/remove-comments-section-from-your-wordpress-blog/" title="Remove Comments Section From Your WordPress Blog">Remove Comments Section From Your WordPress Blog</a></li><li><a href="http://www.phpmagicbook.com/input-output/" title="Input Output in PHP">Input Output in PHP</a></li><li><a href="http://www.phpmagicbook.com/text-to-image-using-php/" title="Text To Image Using PHP">Text To Image Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.185 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-30 16:32:31 -->
