<?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 Simple Tutorials &#187; for</title>
	<atom:link href="http://php.elegosproject.org/tag/for/feed/" rel="self" type="application/rss+xml" />
	<link>http://php.elegosproject.org</link>
	<description>Free and user-friendly PHP tutorials</description>
	<lastBuildDate>Sun, 19 Jul 2009 13:12:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cycles</title>
		<link>http://php.elegosproject.org/2009/06/30/cycles/</link>
		<comments>http://php.elegosproject.org/2009/06/30/cycles/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 17:45:15 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cycle]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=95</guid>
		<description><![CDATA[What are cycles? Commands repeated more than once. Is it usefull to copy/paste the same functions more than once? No.
PHP is able to avoid this, with these functions: for, while, switch, foreach.

So let&#8217;s start with an example:
&#60;?php

echo &#34;Number 1&#60;br /&#62;&#34;;
echo &#34;Number 2&#60;br /&#62;&#34;;
echo &#34;Number 3&#60;br /&#62;&#34;;
echo &#34;Number 4&#60;br /&#62;&#34;;
echo &#34;Number 5&#60;br /&#62;&#34;;
echo &#34;Number 6&#60;br /&#62;&#34;;
echo [...]]]></description>
			<content:encoded><![CDATA[<p>What are cycles? Commands repeated more than once. Is it usefull to copy/paste the same functions more than once? No.<br />
PHP is able to avoid this, with these functions: for, while, switch, foreach.</p>
<p><span id="more-95"></span></p>
<p>So let&#8217;s start with an example:</p>
<pre class="brush: php;">&lt;?php

echo &quot;Number 1&lt;br /&gt;&quot;;
echo &quot;Number 2&lt;br /&gt;&quot;;
echo &quot;Number 3&lt;br /&gt;&quot;;
echo &quot;Number 4&lt;br /&gt;&quot;;
echo &quot;Number 5&lt;br /&gt;&quot;;
echo &quot;Number 6&lt;br /&gt;&quot;;
echo &quot;Number 7&lt;br /&gt;&quot;;
echo &quot;Number 8&lt;br /&gt;&quot;;
echo &quot;Number 9&lt;br /&gt;&quot;;

?&gt;</pre>
<pre class="brush: php;">&lt;?php
for($i = 0; $i &lt; 9; $i++)
	echo &quot;Number $i&lt;br /&gt;&quot;;
?&gt;</pre>
<p>The two codes up here are exactly the same. The first one is a repeated action, the second one is the same repetition, but it&#8217;s less chaotic and better readable. This is the function <strong>for</strong>. As all the cycle functions it doesn&#8217;t require any graphs if the functions that must be executed are one, elsewise you&#8217;ll have to delimit the functions of the cycle in graphs {}. This is an example:</p>
<pre class="brush: php;">&lt;?php
for($i = 0; $i &lt; 9; $i++) {
	echo &quot;Number $i&lt;br /&gt;&quot;;
	$array[] = $i;
}
?&gt;</pre>
<p>Clear? Great.</p>
<p>Returning to the cycle function<strong>for</strong>: it&#8217;s used when you know the exact number of iterations. It requires three parameters: the starting conditional, the ending conditional and the action taken at each iteration. In the example above $i is initialised to 0, then the cycle will go on untill $i will be inferior to 9, and at each iteration $i will be incremented by one (by the way $n++ will firstly return the value of $n and then increment the variable, ++$n will do the same but in the inverse order. Think about $array[$i++] and $array[++$i]).</p>
<p>Well, after the for cycle, here it is the <strong>while</strong> one. It can be used in two different ways:</p>
<pre class="brush: php;">&lt;?php
	$found = false;
	while($found == false) {
		if( // condition //) $found = true;
		else // do something else //
	}

	do {
		// some code here
	} while($found == false);
?&gt;</pre>
<p>As you can see, the while cycle will continue untill the parameter will return TRUE (if $found is false, &#8220;$found == false&#8221; will return true!). In the first case there may be no iteration at all (if $found is true from the beginning, no iteration will be done). In the second case there will be <strong>at least</strong> one iteration, because the check is at the end of the cycle and not at the beginning.<br />
<strong>ATTENTION</strong>: the while cycle (as all the cycles) may go in a &#8220;loop&#8221;, which is a state in which the cycle will run forever. PHP is designed to stop this kind of scripts, but it may load your CPU and RAM before stopping.</p>
<p><strong>switch</strong>: is used to execute some code depending on a variable&#8217;s value. An example is far more easy to understand:</p>
<pre class="brush: php;">&lt;?php
	$myvar = //something dynamically got //;
	switch($myvar) {
		case 0:
			// do something //
			break;
		case 1:
			// do something else //
			break;
		default:
			// if none of the cases are respected,
			// the switch cycle will execute this part.
			break;
	}
?&gt;</pre>
<p>Let&#8217;s start explaining the code: the structure is switch($variable) { cases here }. Cases may be all the types of variables but not arrays, so numbers (integers, floats&#8230;), strings (single characters, phrases) and so on. There are no graphs delimiting the cases, but it&#8217;s used the function &#8220;break&#8221;, which will break indeed the cycle. If you leave the cases without the break in the end, all the functions under the &#8220;succesfull&#8221; case will be run (which isn&#8217;t what you would expect from a switch cycle). The default case is optional, and it&#8217;s the code executed when the value is not one of the scripted cases. In this case, whenever of the default case should miss, nothing will be executed.</p>
<p><strong>foreach</strong>: used to iterate for each element of a pluri-dimensional array. This is an example:</p>
<pre class="brush: php;">&lt;?php
	$data = array(
		0 =&gt; array(
			&quot;var1&quot; =&gt; &quot;a&quot;,
			&quot;var2&quot; =&gt; &quot;b&quot;,
			&quot;var3&quot; =&gt; 6,
		),
		1 =&gt; array(
			&quot;var1&quot; =&gt; &quot;g&quot;,
			&quot;var2&quot; =&gt; &quot;T&quot;,
			&quot;var3&quot; =&gt; 0,
		)
	);

	foreach($data as $case) {
		echo $case['var1'];
	}
?&gt;</pre>
<p>The syntax is foreach($variable as $newname). In this way you&#8217;ll be able to access to the elements of an array variable in a more confortable way instead of doing some maybe difficult while or for cycles (in case of strings as array indexing &#8211;> $array["my index 1"]). In this way $newname will be a new array (or value in case of mono-dimensional arrays), which will have the same behaviour of the mother array, but it will have the values of the internal arrays and not all of them. It&#8217;s normally used to access to XML files (see <a title="php.net strings page" href="http://php.elegosproject.org/2009/06/23/data-storage-read-an-xml-file/" target="_self">Data storage: read an XML file</a> for further informations about it).</p>
<p>For example here it is what $newname will be at the first iteration:</p>
<pre class="brush: php;">$newname = array(
	&quot;var1&quot; =&gt; &quot;a&quot;,
	&quot;var2&quot; =&gt; &quot;b&quot;,
	&quot;var3&quot; =&gt; 6,
);</pre>
<p>Which is the sub-array with index 0 of the mother $data.</p>
<p>It&#8217;s the most complicated cycle to understand, but indeed is pretty simple when you&#8217;ve understood it.</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/30/cycles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
