<?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>Invinciblebrain.com &#187; PHP While Loop</title>
	<atom:link href="http://invinciblebrain.com/tag/php-while-loop/feed" rel="self" type="application/rss+xml" />
	<link>http://invinciblebrain.com</link>
	<description>behold the power of ideas!</description>
	<lastBuildDate>Thu, 08 Jul 2010 17:10:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
    <title>Invinciblebrain.com</title>
    <url>http://invinciblebrain.com/feed-logo.png</url>
    <link>http://invinciblebrain.com</link>
    <width>122</width>
    <height>130</height>
    <description>Invinciblebrain.com - http://invinciblebrain.com</description>
    </image>		<item>
		<title>PHP  While Loop</title>
		<link></link>
		<comments>http://invinciblebrain.com/whats-making-news/php-while-loop.php#comments</comments>
		<pubDate>Sun, 21 Jun 2009 18:22:43 +0000</pubDate>
		<dc:creator>InvincibleBrain</dc:creator>
				<category><![CDATA[invinciblebrain]]></category>
		<category><![CDATA[PHP  While Loop]]></category>
		<category><![CDATA[php & mysql]]></category>
		<category><![CDATA[php mysql technology]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://invinciblebrain.com/?p=110</guid>
		<description><![CDATA[PHP &#8211; While Loop Repetitive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks [...]]]></description>
			<content:encoded><![CDATA[<h1>PHP &#8211; While Loop</h1>
<p>Repetitive tasks are always a burden to us. Deleting spam email, sealing 50  envelopes, and going to work are all examples of tasks that are repeated. The  nice thing about programming is that you can avoid such repetitive tasks with a  little bit of extra thinking. Most often these repetitive tasks are conquered in  the <em>loop</em>.</p>
<p>The idea of a loop is to do something over and over again until the task has  been completed. Before we show a real example of when you might need one, let&#8217;s  go over the structure of the PHP while loop.</p>
<h1>Simple While Loop Example</h1>
<p>The function of the while loop is to do a task over and over as long as the  specified conditional statement is <em>true</em>.  Here is the basic  structure of a PHP while loop:</p>
<div>
<h2>Pseudo PHP Code:</h2>
<pre>while ( conditional statement is true){
	//do this code;
}</pre>
</div>
<p>This isn&#8217;t valid PHP code, but it displays how the while loop is structured.  Here is the break down of how a while loop functions when your script is  executing:</p>
<ol>
<li>The conditional statement is checked. If it is true, then (2) occurs. If    it is false, then (4) occurs.</li>
<li>The code within the while loop is executed.</li>
<li>The process starts again at (1). Effectively &#8220;looping&#8221; back.</li>
<li>If the conditional statement is false, then the code within is not    executed and there is no more looping. The code following the while loop is    then executed like normal.</li>
</ol>
<h1>A Real While Loop Example</h1>
<p>Imagine that you are running an art supply store. You would like to print out  the price chart for number of brushes and total cost. You sell brushes at a flat  rate, but would like to display how much different quantities would cost. This  will save your customers from having to do the mental math themselves.</p>
<p>You know that a while loop would be perfect for this repetitive and boring  task. Here is how to go about doing it.</p>
<div>
<h2>Pseudo PHP Code:</h2>
<pre>$brush_price = 5;
$counter = 10;

echo "&lt;table border=\"1\" align=\"center\"&gt;";
echo "&lt;tr&gt;&lt;th&gt;Quantity&lt;/th&gt;";
echo "&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;";
while ( $counter &lt;= 100 ) {
	echo "&lt;tr&gt;&lt;td&gt;";
	echo $counter;
	echo "&lt;/td&gt;&lt;td&gt;";
	echo $brush_price * $counter;
	echo "&lt;/td&gt;&lt;/tr&gt;";
	$counter = $counter + 10;
}
echo "&lt;/table&gt;";</pre>
</div>
<h2>Display:</h2>
<div>
<table border="1" align="center">
<tbody>
<tr>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td>10</td>
<td>50</td>
</tr>
<tr>
<td>20</td>
<td>100</td>
</tr>
<tr>
<td>30</td>
<td>150</td>
</tr>
<tr>
<td>40</td>
<td>200</td>
</tr>
<tr>
<td>50</td>
<td>250</td>
</tr>
<tr>
<td>60</td>
<td>300</td>
</tr>
<tr>
<td>70</td>
<td>350</td>
</tr>
<tr>
<td>80</td>
<td>400</td>
</tr>
<tr>
<td>90</td>
<td>450</td>
</tr>
<tr>
<td>100</td>
<td>500</td>
</tr>
</tbody>
</table>
</div>
<p>Pretty neat, huh? The loop created a new table row and its respective entries  for each quantity, until our counter variable grew past the size of 100. When it  grew past 100 our conditional statement failed and the loop stopped being used.  Let&#8217;s review what is going on.</p>
<ol>
<li>We first made a $brush_price and $counter variable and set them equal to    our desired values.</li>
<li>The table was set up with the beginning table tag and the table headers.</li>
<li>The while loop <em>conditional statement</em> was checked, and $counter (10)    was indeed smaller or equal to 100.</li>
<li>The code inside the while loop was executed, creating a new table row for    the price of 10 brushes.</li>
<li>We then added 10 to $counter to bring the value to 20.</li>
<li>The loop started over again at step 3, until $counter grew larger than    100.</li>
<li>After the loop had completed, we ended the table.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://invinciblebrain.com/whats-making-news/php-while-loop.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
