<?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>dan.forys.co.uk &#187; programming</title>
	<atom:link href="http://dan.forys.co.uk/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.forys.co.uk</link>
	<description>Dan is a web developer in London. He is interested in all things Internet, Linux and Mac.</description>
	<lastBuildDate>Fri, 22 Jan 2010 14:25:31 +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>Learning PHP &#8211; Part 5: Your first dynamic web page</title>
		<link>http://dan.forys.co.uk/learning-php-part-5-your-first-dynamic-web-page/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-5-your-first-dynamic-web-page/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 08:53:19 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://danforys.com/?p=97</guid>
		<description><![CDATA[After the previous parts of this tutorial, you should be familiar with PHP variables and arrays. Now we will use these concepts to actually make a working web page. This page will contain a form that you can submit, and we will write PHP code to handle the form contents. Step 1: The basic HTML [...]]]></description>
			<content:encoded><![CDATA[<p>After the previous parts of this tutorial, you should be familiar with PHP <a href="/learning-php-part-2-variable-basics/">variables</a> and <a href="/learning-php-part-3-array-basics/">arrays</a>.</p>
<p>Now we will use these concepts to actually make a working web page. This page will contain a form that you can submit, and we will write PHP code to handle the form contents.</p>
<p><span id="more-97"></span></p>
<h3>Step 1: The basic HTML</h3>
<p>This is going to be a real, albeit ugly, web page. So we need some boilerplate HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>My first dynamic web page<span style="color: #339933;">!&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>body
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Save the file as something like form.php and upload it to your web server. Hopefully if you load it, you&#8217;ll see a blank page with the title &#8220;My first dynamic web page!&#8221; </p>
<h3>Step 2: The HTML Form</h3>
<p>There are essentially two types of HTML forms:</p>
<ul>
<li>POST &#8211; when submitted, the contents of the form are sent in the HTTP request body. Using this method will normally result in a prompt if the user presses their browser back button after submitting the form. POST requests cannot be bookmarked</li>
<li>GET &#8211; when submitted, the contents of the form will appear in the URL in the browser address bar. These addresses can be bookmarked by the user.</li>
</ul>
<p>For our page, we&#8217;ll use a POST method form. This code should be inserted between the body tags at line 7:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-name&quot;</span><span style="color: #339933;">&gt;</span>Name<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-name&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-name&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-message&quot;</span><span style="color: #339933;">&gt;</span>Message<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-message&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user-message&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Submit&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Note the blank action property of the form tag &#8211; this means that when the user submits the form, the browser will POST the contents back to the same page. Alternatively, if we wanted to use a different page, its URL could have been entered here.</p>
<p>If you upload and view the page in your browser, you should be able to see the form. Feel free to enter some values and submit the form. As you&#8217;ll see, the form simply goes back to the same page.</p>
<h3>Step 3: Enter the PHP</h3>
<p>PHP has a magic array called $_POST that contains any values submitted using an HTTP POST. It&#8217;s very straightforward to use, simply being an associative array. Each key in the array is the form &#8220;name&#8221; attribute and each value is the corresponding data entered by the user.</p>
<p>For example, for our form above:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user-name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>will output whatever the user has entered in the name field on our form, when it is submitted.</p>
<p>There&#8217;s an important note about PHP&#8217;s error handling with arrays:</p>
<blockquote><p>If you attempt to access an array key that doesn&#8217;t exist, PHP might show an error message (more specifically a <em>notice</em>). These notices will show if you have PHP configured to show its strictest errors. For this reason, it&#8217;s good practice to either pre-populate your array with empty values; or check if a key exists before you attempt to use it.</p></blockquote>
<p>With this warning in mind, let&#8217;s add some code to the very start of the file, insert the following at line 1:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user-name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user-name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user-message'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user-message'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Lines 2 and 3 simply prepare two variables for us to use later, this is called <em>initialisation</em>. It is good practice to get into the habit of doing this so that you know exactly what the starting values of the variables are going to be.</p>
<p>Lines 4 and 5 check to see if the keys &#8216;user-name&#8217; and user-message&#8217; exist in the $_POST array. If the user had submitted the form, the values $_POST['user-name'] and $_POST['user-message'] will contain the text the user entered.</p>
<h3>Using the submitted data</h3>
<p>Finally, lets embed some echo statements to show the user some feedback. The finished script will look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user-name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user-name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user-message'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user-message'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My first dynamic web page!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;p&gt;&lt;label for=&quot;user-name&quot;&gt;Name: &lt;/label&gt;&lt;input id=&quot;user-name&quot; name=&quot;user-name&quot; value=&quot;&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;user-message&quot;&gt;Message: &lt;/label&gt;&lt;input id=&quot;user-message&quot; name=&quot;user-message&quot; value=&quot;&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;
&nbsp;
&lt;p&gt;User name is <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
&lt;p&gt;Message is <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
&nbsp;
&lt;/body
&lt;/html&gt;</pre></td></tr></table></div>

<p>Lines 19 and 20 echo out the user data. If you save and run the script, you should be able to put text into the form, submit it and have the results output on the page.</p>
<h3>Persisting form values</h3>
<p>Finally, we can modify lines 14 and 15 to echo out the user text as the input &#8216;values&#8217;. This has the effect of keeping the user input in the form fields when the form is submitted:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;p&gt;&lt;label for=&quot;user-name&quot;&gt;Name: &lt;/label&gt;&lt;input id=&quot;user-name&quot; name=&quot;user-name&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;user-message&quot;&gt;Message: &lt;/label&gt;&lt;input id=&quot;user-message&quot; name=&quot;user-message&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;</pre></td></tr></table></div>

<h3>Final thoughts</h3>
<p>You now have the basics to build simple dynamic web pages. In the next tutorials we&#8217;ll go on to more advanced PHP features.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-5-your-first-dynamic-web-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part 4: Controlling flow</title>
		<link>http://dan.forys.co.uk/learning-php-part-4-controlling-flow/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-4-controlling-flow/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 22:22:07 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://danforys.com/?p=108</guid>
		<description><![CDATA[So now we&#8217;ve looked at variables and arrays, it&#8217;s time to make the code a little bit smarter. We&#8217;ve already seen how the foreach() can loop (iterate) over items in an array, but there are other ways to alter the flow of the code as well. Conditional One of the most common PHP statements is [...]]]></description>
			<content:encoded><![CDATA[<p>So now we&#8217;ve looked at variables and arrays, it&#8217;s time to make the code a little bit smarter.</p>
<p>We&#8217;ve already seen how the foreach() can loop (iterate) over items in an array, but there are other ways to alter the flow of the code as well.</p>
<h3>Conditional</h3>
<p>One of the most common PHP statements is the &#8220;if&#8221;. In a nutshell, &#8220;if&#8221; does the following:</p>
<blockquote><p>
if (some condition is true) { run this code }
</p></blockquote>
<p><span id="more-108"></span></p>
<p>This means that if whatever is in the brackets is &#8220;true&#8221;, PHP will run the code in the curly braces. If the condition is not true, PHP will ignore the code in the curly braces.</p>
<p>Here&#8217;s a simple example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The number is 1!'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The number is 5!'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the script is run, it will output &#8216;The number is 5!&#8217;. </p>
<p>Note the double equals in the conditions (==). There is an important distinction to make between the &#8216;==&#8217; and the &#8216;=&#8217;:</p>
<ul>
<li>&#8216;==&#8217; is used to compare one thing to another. If the &#8216;things&#8217; are equal, it will evaluate to true. Otherwise it evaluates to false.</li>
<li>&#8216;=&#8217; is used to assign values to variables. (This is actually a simplification, we will come back to its actual meaning in a later post. However, for now you can assume that this is the case)</li>
</ul>
<p>So, line 2 in the example is saying &#8220;$number == 1 evaluates to true, run the rest of the code&#8221;. On line 1, we have set &#8220;$number = 5&#8243; making the condition evaluate to false, meaning that &#8220;The number is 1!&#8221; is not output.</p>
<p>However, on line 3, the condition &#8220;$number == 5&#8243; evaluates to true, resulting in the string being output.</p>
<p>In place of the ==, you can also use other operators such as &gt; (true if the left is greater than the right), and &lt; (true if the left is less than the right). For example, to test if a number is between 10 and 20:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">9</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Win!'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The double ampersand &#8216;&#038;&#038;&#8217; means a logical AND, that is the whole expression is true only if the left expression AND the right expression are true. Although we&#8217;re looking for a number between 10 and 20, note that in the expressions we are using 9 and 21. This is because we are testing for numbers greater than 9 and less than 21 &#8211; this doesn&#8217;t include the numbers themselves.</p>
<p>PHP also has >= &lt;= operators; greater than or equals, less than or equals respectively. The same result as the example above looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Win!'</span><span style="color: #339933;">;</span></pre></div></div>

<p>The conditions needn&#8217;t be limited to variables; you can use anything that can evaluate to true or false. PHP has lots of functions that can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$someVariable</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The variable is set'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8216;isset&#8217; is a PHP function that returns true if a variable has been defined, or false if not. In this example we&#8217;re asking it to tell us if the $someVariable is set.</p>
<h3>The &#8216;while&#8217; loop</h3>
<p>There is a way of looping through code &#8216;while&#8217; a particular condition is true:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$counter</span> <span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// This is the same as $counter = $counter + 1</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Counter is '</span><span style="color: #339933;">.</span><span style="color: #000088;">$counter</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code loops around ten times, incrementing the counter each time and outputting it to the screen.</p>
<blockquote><p>Note that if the condition is <strong>false</strong> at the start, the while loop is never executed</p></blockquote>
<h3>The &#8216;for&#8217; loop</h3>
<p>The for loop is one of the most useful and powerful looping structures in PHP. Its syntax looks rather opaque at first glance, but you&#8217;ll soon get used to it. The syntax looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</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: #0000ff;">'Index is '</span><span style="color: #339933;">.</span><span style="color: #000088;">$index</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The &#8216;for&#8217; statement itself has three parts, each separated by a semicolon. The first part initialises a variable for us to control the loop; in this case we are setting it to zero. The second part has a condition that will cause the loop to continue as long as it evaluates to true. The third and final part is the operation that gets performed each time the loop repeats; in this case it is incrementing the value of $index.</p>
<p>The result of the code is almost identical to the while loop &#8211; it will loop around ten times and echo out the index each time.</p>
<h3>The foundation of PHP programming</h3>
<p>If you&#8217;ve followed the tutorial up to this point, you now have the foundations to build your first dynamic web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-4-controlling-flow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.742 seconds -->
