<?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>Webcodez - The database of web programming tutorials &#187; loops</title>
	<atom:link href="http://www.webcodez.net/tag/loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Sun, 14 Aug 2011 14:01:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>PHP While &#8211; Do While Loop</title>
		<link>http://www.webcodez.net/php-mysql/php-while-do-while-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/php-while-do-while-loop/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 20:19:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[do while loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=632</guid>
		<description><![CDATA[The While Loop is used to repeat executing a certain code as long as a given condition is (still) true. while( … condition … ) { … execute code … } The code between accolades is only executed when the condition between brackets is true ( just like the If Loop) however after it executed [...]]]></description>
			<content:encoded><![CDATA[<p>The While Loop is used to repeat executing a certain code as long as a given condition is (still) true.</p>
<pre name="code" class="php">
while( … condition … ) {
	… execute code …
}
</pre>
<p>The code between accolades is only executed when the condition between brackets is true ( just like the If Loop) however after it executed the code, it will go back to check the condition again; if it’s still true, it will execute the code again, etc.. This will be repeated until the condition between brackets is no longer true. Only then, it will stop executing the code and continue with the rest of your script ( below the while loop ). Therefore, you should watch out for endless while loops ( which keep repeating themselves for an infinitive time ), for ex.:</p>
<pre name="code" class="php">
$num = 1;
while($num == 1) {
	echo “Num equals 1.”;
}
</pre>
<p>As num is always equal to 1, this code will be executed forever. When that happens, the rest of your script is no longer executed and the browser will be stuck in executing the while loop script which will cause the browser to crash ( mostly ). A valid example of the use of a while loop could be for ex.:</p>
<pre name="code" class="php">
$num = 1;
while($num &lt;= 10) {
	echo “&lt;p&gt;”.$num.”&lt;/p&gt;”;
	$num++;
}
</pre>
<p>Which shows the value of $num as long as it’s not greater than 10 and then increases it by 1. In other words: it counts to 10.</p>
<p>Now, another form of the while loop is the do while loop. The difference with the while loop is that the code of the do while loop is atleast executed ONCE ( no matter if the condition is true or not ). For example:</p>
<pre name="code" class="php">
$num = 1;
do {
	echo “&lt;p&gt;”.$num.”&lt;/p&gt;”;
	$num++;
}while($num &lt;= 0);
</pre>
<p>Will result in displaying the number 1. However when we’d use the while loop instead, it would result in displaying nothing. The code would not be executed because the variable $num equals 1, which is greater than 0. The condition is false so the code won’t be executed ( not once ) in a while loop. However in a do while loop it executes it at least once but also after that it will check the condition and no longer execute the code when this condition is false.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-while-do-while-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Foreach Loop</title>
		<link>http://www.webcodez.net/php-mysql/php-foreach-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/php-foreach-loop/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 20:05:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[foreach loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=629</guid>
		<description><![CDATA[A total different kind of loop is the foreach loop. This loop is used to separately handle each sub-variable out of an array. Each sub-variable may be split into its name and its value. foreach($array AS $key =&#62; $value) { ... execute code ... } Each sub-variable of the array $array will be split into [...]]]></description>
			<content:encoded><![CDATA[<p>A total different kind of loop is the foreach loop. This loop is used to separately handle each sub-variable out of an array. Each sub-variable may be split into its name and its value. </p>
<pre name="code" class="php">
foreach($array AS $key =&gt; $value) {
	... execute code ...
}
</pre>
<p>Each sub-variable of the array $array will be split into its key (stored into $key) and its value (stored into $value) and may be used in the code that’s to be executed ( between the accolades ). It’s a very efficient way to loop through all sub-variables of an array. Each sub-variable will have its key and value stored into separate variables and will go through the code between accolades. Example:</p>
<pre name="code" class="php">
$products = new array(
			0 =&gt; “product example”,
			1 =&gt; “another product”
		         );
foreach($products as $id =&gt; $name) {
	echo “Product Id: “.$id.” &lt;br /&gt;”;
	echo “Product Name: “.$name.” &lt;p&gt;”;
}</pre>
<p>The output of this script will be:</p>
<blockquote><p>Product Id: 0<br />
Product Name: product example<br />
Product Id: 1<br />
Product Name: another product</p></blockquote>
<p>Do realize that the variables inside the foreach loop ( between brackets ) may be given any names. It’s also possible to only use the values of the sub-variables if you don’t need the keys:</p>
<pre name="code" class="php">
$products = new array(
			0 =&gt; “product example”,
			1 =&gt; “another product”
		         );
foreach($products as $name) {
	echo “Product Name: “.$name.” &lt;br /&gt;”;
}
</pre>
<p>The output of this script will be:</p>
<blockquote><p>Product Name: product example<br />
Product Name: another product</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-foreach-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP For Loop</title>
		<link>http://www.webcodez.net/php-mysql/php-for-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/php-for-loop/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 20:00:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=624</guid>
		<description><![CDATA[The For loop is used to repeat executing a certain code for a specific amount of times. This could as well be done with the while loop however the for loop is made for this. The while loop may be used for much more complex stuff and for various other purposes. The for loop is [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>For </strong>loop is used to repeat executing a certain code for a specific amount of times. This could as well be done with the while loop however the for loop is made for this. The while loop may be used for much more complex stuff and for various other purposes. The for loop is sorely used for repeating.</p>
<pre name="code" class="php">
for( … declaration …; … condition …; …action… ) {
	… execute code …
}
</pre>
<p>The arguments between brackets are 3: a declaration, a condition and an action. The declaration is usually an integer variable containing a start value, which is usually increased in the action. For example, we could make a for loop which is repeated 10 times:</p>
<pre name="code" class="php">
for($i=1;$i&lt;=10;$i++) {
	echo “&lt;p&gt;”.$i.”&lt;/p&gt;”;
}
</pre>
<p>This will result in displaying the numbers 1 through 10.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP If Loop</title>
		<link>http://www.webcodez.net/php-mysql/php-if-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/php-if-loop/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 18:50:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[if loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=611</guid>
		<description><![CDATA[The If Loop is a very basic loop and used often. It’s used to check a condition and execute a code based on whether the condition was true or not. if( … condition … ) { … execute code … }else{ … execute other code … } The condition is put between brackets and the [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>If </strong>Loop is a very basic loop and used often. It’s used to check a condition and execute a code based on whether the condition was true or not.</p>
<pre name="code" class="php">
if( … condition … ) {
	… execute code …
}else{
	… execute other code …
}
</pre>
<p>The condition is put between brackets and the code between accolades. The first code is only executed when the condition between brackets is valid ( true ), otherwise the 2nd code is executed.<br />
For example:</p>
<pre name="code" class="php">
$num = 1;
if($num == 1) {
	echo “The variable num is equal to 1.”;
}else{
	echo “The variable num is not equal to 1.”;
}
</pre>
<p>The above code will return:</p>
<blockquote><p>The variable num is equal to 1.</p></blockquote>
<p>Because $num == 1 is true and so the first code(the if-part of the loop) is executed. If num was not equal to 1 the 2nd code would have been executed ( the else-part of the loop ).</p>
<p>If you only want to execute a code if the condition is true, and don’t do anything if it isn’t, then you can leave out the else-part. Like this:</p>
<pre name="code" class="php">
//execute a code if $num == 1
if($num == 1) {
	echo “The variable num is equal to 1.”;
}
//otherwise do nothing
</pre>
<p>Sometimes, checking one condition is not enough. Therefore you can use <strong>else if</strong> -parts for your loop.</p>
<pre name="code" class="php">
if($rank == “admin”) {
	echo “Welcome Admin!”;
}else if($rank == “moderator”) {
	echo “Welcome moderator!”;
}else if($rank == “guest”) {
	echo “You’re not allowed to be here, guest!”;
}
</pre>
<p>The above code, for example, has multiple conditions. If the first condition is true, it will execute the first code between accolades, if the second condition is true, it will execute the second code between accolades, etc.. Do notice it will check the conditions in the order of how they’re written down. So it will first check the first condition and if that condition is true, it will not check the other conditions in the rest of the else if -parts anymore. It will only execute ONE ( or none ) of the codes between accolades. When multiple conditions may be true and so you want to check all the conditions and make it possible to have multiple codes executed, then multiple separate if loops should be used instead of the else if-parts.</p>
<p>It’s also possible to have multiple comparisions within one condition. These are the operators that may be used:</p>
<p>[TABLE=12]</p>
<p>And for comparing:</p>
<p>[TABLE=13]</p>
<p>The difference between == and = is that == is used to compare variables/values, and = is used to set a variable equal to a value.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-if-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Loops</title>
		<link>http://www.webcodez.net/php-mysql/php-loops/</link>
		<comments>http://www.webcodez.net/php-mysql/php-loops/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 18:42:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[do while loop]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[foreach loop]]></category>
		<category><![CDATA[if loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=603</guid>
		<description><![CDATA[In PHP loops are used to execute codes under certain conditions and may also be used to repeat the code. There are different loops to do different kind of things: If loop The If loop is used to execute a certain code only when a certain condition is true. While loop The While loop is [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP loops are used to execute codes under certain conditions and may also be used to repeat the code. There are different loops to do different kind of things:</p>
<p><strong><a href="http://www.webcodez.net/php-mysql/php-if-loop" target="_self">If loop</a></strong><br />
The If loop is used to execute a certain code only when a certain condition is true.</p>
<p><strong><a href="http://www.webcodez.net/php-mysql/php-while-do-while-loop" target="_self">While loop</a></strong><br />
The While loop is used to execute a certain code only when a condition is true, and repeat the code as long as the condition is still true.</p>
<p><strong><a href="http://www.webcodez.net/php-mysql/php-while-do-while-loop" target="_self">Do While loop</a></strong><br />
The Do While loop is used to execute a certain code and repeat it as long as a condition is true. The difference with the while loop is that it executes the code at least once, then checks the condition for further repeating.</p>
<p><strong><a href="http://www.webcodez.net/php-mysql/php-for-loop" target="_self">For loop</a></strong><br />
The For loop is used to repeat executing a code a certain amount of times. It does not only consist of a condition, but also a declaration of a variable and an action to perform. For example an integer would be declared and the action would be to increase the integer by 1 each time the loop’s code has been executed. Then a condition could be to execute the code as long as this variable is not greater than 10, to execute the code 10 times.</p>
<p><strong><a href="http://www.webcodez.net/php-mysql/php-foreach-loop" target="_self">Foreach loop</a></strong><br />
This is a somewhat different kind of loop. It may be used to handle each sub-variable of an array apart, seperating the key and belonging value. This makes it easy to loop through all sub-variables ( all combinations of a key and a value ) of an array.</p>
<p>
Each of these loops are explained inside apart references. Click on the title of the loop to get a reference of that specific loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/php-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

