<?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</title>
	<atom:link href="http://dan.forys.co.uk/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>Fixing a bricked D-Link DSL-G624T</title>
		<link>http://dan.forys.co.uk/fixing-a-bricked-d-link-dsl-g624t/</link>
		<comments>http://dan.forys.co.uk/fixing-a-bricked-d-link-dsl-g624t/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 14:25:31 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[dlink]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[modem]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://danforys.com/?p=164</guid>
		<description><![CDATA[My Parents have a D-Link DSL-G624T ADSL modem/switch. Unfortunately, my brother decided to update its firmware, crashing it in the process. After a bit of remote debugging over the phone, we thought the best option was to send it to me. The symptoms were as follows: The power LED came on as normal, the status [...]]]></description>
			<content:encoded><![CDATA[<p>My Parents have a D-Link DSL-G624T ADSL modem/switch. Unfortunately, my brother decided to update its firmware, crashing it in the process. After a bit of remote debugging over the phone, we thought the best option was to send it to me.</p>
<p>The symptoms were as follows: The power LED came on as normal, the status LED was flashing as normal &#8211; indicating that it was at least booting up. The LAN link LEDs would light up when an ethernet cable was plugged in. However, its web administration panel couldn&#8217;t be reached on http://192.168.1.1 and it couldn&#8217;t be pinged either.</p>
<p>After a lot of searching, reading and failed attempts at repairing it, I have found a method to reset the firmware without the web interface. Quite why D-Link make it so awkward for their UK customers, I don&#8217;t know.<br />
<span id="more-164"></span></p>
<blockquote><p><strong>DISCLAIMER</strong> Follow these instructions at your own risk; these instructions worked for me, I make no guarantees it will work for you. </p>
<p>I did this on a revision &#8216;A&#8217; device &#8211; you&#8217;ll need to download different firmware if you have a revision &#8216;B&#8217; or &#8216;C&#8217; device. </p></blockquote>
<p>I reproduce all the steps to manually recreate the firmware update package below. For convenience, I have packaged everything up. If you use my <a href='http://danforys.com/wp-content/uploads/2010/01/D-Link-G624T-rescue.zip'>D-Link G624T rescue package</a>, you can skip straight to steps 4 and 5.</p>
<p><strong>Step 1: Download the Australian firmware package for the G604T.</strong></p>
<p>At the time of writing you can find it on <a href="ftp://files.dlink.com.au/products/DSL-G604T/REV_A/Firmware//Firmware_V2.00B12.AU_20070509_EXE/DSL-G604T_V2.00B12.AU_20070509.zip">their FTP server</a>. It&#8217;s important to get the EXE version which contains the program to reset the device.</p>
<p>Extract the .zip archive and run the program. A window will appear asking you for the device&#8217;s IP address. At this point, open an explorer window and navigate to your user&#8217;s &#8216;temp&#8217; folder (in my case C:\Documents and Settings\Dan\Local Settings\Temp &#8211; you will need to turn on &#8216;show hidden files&#8217; to see it). In here will be a randomly named folder (something like &#8216;ckz_DE0G&#8217;) with a program called TIupdate.exe in it:</p>
<p><a href="http://danforys.com/wp-content/uploads/2010/01/d-link-firmware-update.png"><img src="http://danforys.com/wp-content/uploads/2010/01/d-link-firmware-update.png" alt="" title="d-link-firmware-update" width="364" height="110" class="alignnone size-full wp-image-176" /></a></p>
<p>Take a copy of the files in the folder and put them in a folder on your desktop called &#8216;update&#8217;.  Once you&#8217;ve taken a copy, you can close the program.</p>
<p>We won&#8217;t need the Australian firmware files, so delete &#8216;config.DLinkAU_DSL-G604T.img&#8217; and &#8216;DLinkAU_DSL-G604T_singleimage_kernel_fs_V2.00B12.AU_20070509&#8242;. You should be left with the following:</p>
<blockquote><p>external_boot.bin<br />
tiupgrade.exe<br />
tiupgrade.ini<br />
tiupgrade.scp</p></blockquote>
<p><strong>Step 2: Download the UK firmware</strong></p>
<p>Grab the <a href="ftp://ftp.dlink.co.uk//dsl_routers_modems/dsl-g624t/DLinkUK-A_DSL-G624T_V3.10B01T02.UK-A.20070628.zip">latest DSL-G624T firmware</a> from the D-Link support site and extract it on your desktop.</p>
<p>From the resulting files, copy the two files inside the &#8216;image&#8217; folder into the &#8216;upgrade&#8217; folder you created previously. Also, open the mtd-4m.txt file in Notepad, you&#8217;ll need this in a moment.</p>
<p><strong>Step 3: Set the firmware uploader to use the UK firmware</strong></p>
<p>In the &#8216;upgrade&#8217; folder, open the &#8216;tiupgrade.scp&#8217; file in notepad. You should see something like the following:</p>
<blockquote><p>etenv mtd0 0&#215;90091000,0x903f0000<br />
setenv mtd1 0&#215;90010090,0&#215;90090000<br />
setenv mtd2 0&#215;90000000,0&#215;90010000<br />
setenv mtd3 0x903f0000,0&#215;90400000<br />
setenv mtd4 0&#215;90010000,0x903f0000<br />
setenv autoload 1<br />
setenv StaticBuffer 120<br />
setenv modulation MMODE<br />
setenv autoload_timeout 15<br />
setenv StaticBuffer 384<br />
setenv SW_FEATURES 0X8000<br />
reboot<br />
reconnect 3<br />
put config.DLinkAU_DSL-G604T.img mtd3<br />
put DLinkAU_DSL-G604T_singleimage_kernel_fs_V2.00B12.AU_20070509 mtd4<br />
setenv autoload_timeout 5<br />
reboot</p></blockquote>
<p>This is a script telling the firmware uploader how to apply the firmware to the router. You need to amend this with the contents of the &#8216;mtd-4m.txt&#8217; you have open. You also need to amed the two &#8216;put&#8217; lines to reflect the filenames of the UK firmware. </p>
<p>After editing it, my tiupgrade.scp looked like this: (edited lines in bold)</p>
<blockquote><p><strong>env mtd0 0x900a1000,0x903f0000<br />
setenv mtd1 0&#215;90010090,0x900a1000<br />
setenv mtd2 0&#215;90000000,0&#215;90010000<br />
setenv mtd3 0x903f0000,0&#215;90400000<br />
setenv mtd4 0&#215;90010000,0x903f0000</strong><br />
setenv autoload 1<br />
setenv StaticBuffer 120<br />
setenv modulation MMODE<br />
<strong>setenv autoload_timeout 120</strong><br />
<strong>setenv StaticBuffer 120</strong><br />
setenv SW_FEATURES 0X8000<br />
reboot<br />
reconnect 3<br />
<strong>put config.DLinkUK-A_DSL-G624T.img mtd3<br />
put DLinkUK-A_DSL-G624T_singleimage_kernel_fs_V3.10B01T02.UK-A.20070523 mtd4</strong><br />
setenv autoload_timeout 5<br />
reboot</p></blockquote>
<p>Save the file and you&#8217;re almost ready to go.</p>
<p><strong>Step 4: Set up your network</strong></p>
<p>To ensure your PC can talk to the router correctly, you need to make sure you have a static IP address on the same subnet. By default, the UK version of the router is on 192.168.1.1.</p>
<p>Go into your LAN properties: Start -> Control Panel -> Local Area Connection (or similar). Right click and choose &#8216;properties&#8217;. Double click on the &#8216;Internet Protocol (TCP/IP) in the list to bring up the IP properties. Set a static IP address on the same subnet as the router, for example:</p>
<p><a href="http://danforys.com/wp-content/uploads/2010/01/windows-networking.png"><img src="http://danforys.com/wp-content/uploads/2010/01/windows-networking-300x296.png" alt="" title="windows-networking" width="300" height="296" class="alignnone size-medium wp-image-174" /></a></p>
<p><strong>Step 5: Upload and activate the firmware</strong></p>
<p>Now for the easy bit. Plug the router into your computer with an ethernet cable and ensure the router is switched on. Run the &#8216;tiupgrade.exe&#8217; program. You should see a window like this:</p>
<p><a href="http://danforys.com/wp-content/uploads/2010/01/d-link-firmware-program.png"><img src="http://danforys.com/wp-content/uploads/2010/01/d-link-firmware-program-300x132.png" alt="" title="d-link-firmware-program" width="300" height="132" class="alignnone size-medium wp-image-175" /></a></p>
<p>Ensure the &#8216;Corrupted-image mode&#8217; checkbox is checked, then click &#8216;Next&#8217;. The program will prompt you to power-down the router, wait 10 seconds, then click &#8216;Next&#8217;. Carefully follow these instructions and the firmware update should start automatically (It takes a few minutes).</p>
<p>Once you&#8217;ve done that, the router should reboot, and hopefully work again. It will have been reset back to factory settings, so you should be able to log in at http://192.168.1.1 with username &#8216;admin&#8217; and password &#8216;admin&#8217;.</p>
<p>Good luck, I hope this helps someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/fixing-a-bricked-d-link-dsl-g624t/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part 6: Functions</title>
		<link>http://dan.forys.co.uk/learning-php-part-6-functions/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-6-functions/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 22:19:28 +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=150</guid>
		<description><![CDATA[Functions are a very important PHP feature, and are the building blocks to some very powerful programming techniques. First of all, what is a function? In the tutorials so far, the PHP scripts have run from the top down. All instructions are performed in the order they appear in the file. A function can be [...]]]></description>
			<content:encoded><![CDATA[<p>Functions are a very important PHP feature, and are the building blocks to some very powerful programming techniques. First of all, what is a function?</p>
<p>In the tutorials so far, the PHP scripts have run from the top down. All instructions are performed in the order they appear in the file.</p>
<blockquote><p>A function can be thought of as a reusable &#8216;black box&#8217;. You (optionally) give it some data (called arguments or parameters), it performs an operation and (optionally) gives you a result.</p></blockquote>
<p>This is illustrated in the following diagram:<br />
<img src="http://danforys.com/wp-content/uploads/2009/11/function.png" alt="function" title="function" width="237" height="270" class="alignnone size-full wp-image-151" /></p>
<p><span id="more-150"></span></p>
<p>We have already used a function in previous examples; namely &#8216;isset&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$isTheVariableSet</span> <span style="color: #339933;">=</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: #339933;">;</span></pre></div></div>

<p>In this case, isset takes one argument ($someVariable) and gives you a value of true or false back depending on whether the variable is set or not. This true or false value is assigned the $isTheVariableSet variable in the example.</p>
<blockquote><p>Functions calls are always written using the function name, followed by opening and closing brackets. The arguments go in the brackets, separated by commas.</p></blockquote>
<h3>Anatomy of a function</h3>
<p>PHP has hundreds of functions built-in, but if a native function won&#8217;t solve your problem  you can build your own.</p>
<p>It is good programming practice to keep function definitions simple: make your function do one thing. You&#8217;ll make your code easier to maintain if you keep things simple.</p>
<p>User defined functions take the following form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> myFunctionName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$argument1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$argument2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Your function code goes here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Firstly, you need a specify a name for your function. The name has the same limitations as variables (must start with a letter, or underscore, followed by any number of letters, numbers or underscores). Be sure to call your function something meaningful that describes what it does, avoid names like function1.</p>
<p>Secondly, within the brackets, you specify what inputs (arguments/parameters) you need for your function to work. In PHP the order of these is significant; when calling your function you will need to specify the same arguments in the same order.</p>
<p>Finally, you write the code to make the function work within the curly braces.</p>
<h3>Your first function</h3>
<p>Let&#8217;s start with a really trivial example. A function to generate a string to greet somebody. We&#8217;ll call it getGreetingFor(). We&#8217;ll give it one argument &#8211; the name of the person to greet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getGreetingFor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$greeting</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Hello '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$greeting</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The function is called like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> getGreetingFor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'Hello Dan'</span></pre></div></div>

<p>When the above code is run, the string &#8216;Dan&#8217; is assigned to the variable $name inside our function definition. The code inside the function concatenates &#8216;Hello &#8216; with the contents of $name, then assigns it to a new variable called $greeting.</p>
<p>The &#8216;return&#8217; keyword immediately exits the function code and passes back any value associated with it. In this case, it returns the value of $greeting (&#8216;Hello Dan&#8217; in this particular example).</p>
<p>The returned value is then used by the &#8216;echo&#8217; statement to output the string on the page.</p>
<h3>Variable scope</h3>
<p>At this point, it&#8217;s useful to explain PHP&#8217;s concept of variable scope. Scope can be explained in simple terms:</p>
<blockquote><p>Any variables used inside a function are (usually) not visible outside the function. Likewise, variables declared outside the function are (usually) not visible inside the function.</p></blockquote>
<p>This can be demonstrated using the example above:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getGreetingFor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$greeting</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Hello '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$greeting</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> getGreetingFor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Greg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'Hello Greg'</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs nothing</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$greeting</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs nothing</span></pre></td></tr></table></div>

<p>The variables $name and $greeting used inside the function are not visible outside the function. Instead, you must pass variables in using arguments and get a value out using the return statement. Again, think of functions as self-contained black boxes.</p>
<h3>More examples</h3>
<p>Functions can have multiple arguments:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> areNamesTheSame<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name2</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;">$name1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$name2</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>areNamesTheSame<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Philbert'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Jeremy'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Philbert and Jeremy are the same'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs nothing</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>areNamesTheSame<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Dan'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Dan and Dan are the same'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs 'Dan and Dan are the same'</span></pre></td></tr></table></div>

<p>Note that if the names are the same, the return statement on line 2 will exit the function and return true &#8211; line 3 is never reached. Conversely, if the names are not the same, it will reach line 3 and return false.</p>
<p>You could use a function to count down from a number:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// initialise the string</span>
    <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: #000088;">$number</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">--</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Adds the current number and a space to $countdownString</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$countdownString</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// outputs '10 9 8 7 6 5 4 3 2 1 0 '</span>
<span style="color: #b1b100;">echo</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// outputs '3 2 1 0 '</span></pre></td></tr></table></div>

<h3>Arguments with default values</h3>
<p>You can make arguments optional by supplying default values. Let&#8217;s rewrite the countDown() function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$finish</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// initialise the string</span>
    <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: #000088;">$start</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$finish</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">--</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$countdownString</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$countdownString</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs '10 9 8 7 6 5 4 3 2 1 0' same as before</span>
<span style="color: #b1b100;">echo</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs '10 9 8 7 6 5'</span>
<span style="color: #b1b100;">echo</span> countDown<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs '5 4 3 2 1 0 -1 -2 -3 -4 -5'</span></pre></td></tr></table></div>

<p>In this case, the $finish argument is optional because it has a default value of 0. This means that if we don&#8217;t supply the second argument in the function call, $finish has the value 0.</p>
<blockquote><p>Note that default parameters must be listed last in the function definition. You can&#8217;t for example declare a function like this: function countDown($finish = 0; $start)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-6-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Symptoms of a WordPress Hack</title>
		<link>http://dan.forys.co.uk/symptoms-of-a-wordpress-hack/</link>
		<comments>http://dan.forys.co.uk/symptoms-of-a-wordpress-hack/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 21:06:35 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://danforys.com/?p=119</guid>
		<description><![CDATA[I came to my site on Sunday morning ready to write the latest in my PHP tutorial, only to find a nasty looking 500 server error. I couldn&#8217;t even log into the admin panel. After a while tinkering with various configuration settings and trying to get something to come up, I started searching the web. [...]]]></description>
			<content:encoded><![CDATA[<p>I came to my site on Sunday morning ready to write the latest in my PHP tutorial, only to find a nasty looking 500 server error. I couldn&#8217;t even log into the admin panel.</p>
<p>After a while tinkering with various configuration settings and trying to get something to come up, I started searching the web. Turns out, earlier versions of WordPress were vulnerable to specific hacks that could let attackers create their own admin users.</p>
<p><span id="more-119"></span></p>
<p>Looking in the WordPress users table, three new users had been created. Their first names were set to the following: (note: code deliberately broken so I don&#8217;t trigger security protections)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
&nbsp;
<span style="color: #339933;">&lt;</span>b id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;user_superuser&quot;</span><span style="color: #339933;">&gt;&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;JavaScript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> setUserName <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> t<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;user_superuser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>t.<span style="color: #660066;">nodeName</span><span style="color: #339933;">!=</span><span style="color: #3366CC;">&quot;TR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    t<span style="color: #339933;">=</span>t.<span style="color: #660066;">parentNode</span><span style="color: #339933;">;</span>\n               <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
               t.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> tags <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;H3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot; shown below&quot;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> tags.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> t<span style="color: #339933;">=</span>tags<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> h<span style="color: #339933;">=</span>tags<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>t.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                         s <span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>s<span style="color: #339933;">;</span>
                         h.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>h.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                         t <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                         h.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> arr<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> arr<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;subsubsub&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		    <span style="color: #003366; font-weight: bold;">var</span> n<span style="color: #339933;">=</span><span style="color: #009966; font-style: italic;">/&gt;Administrator \\((\\d+)\\)&lt;/gi</span>.<span style="color: #660066;">e</span><span style="color: #339933;">-</span>xec<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> txt<span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&gt;Administrator \\((\\d+)\\)&lt;/gi</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&gt;Administrator (&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&lt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>txt<span style="color: #339933;">;</span>
		    <span style="color: #009900;">&#125;</span>
&nbsp;
		    <span style="color: #003366; font-weight: bold;">var</span> n<span style="color: #339933;">=</span><span style="color: #009966; font-style: italic;">/&gt;Administrator &lt;span class=&quot;count&quot;&gt;\\((\\d+)\\)&lt;/gi</span>.<span style="color: #660066;">e</span><span style="color: #339933;">-</span>xec<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> txt<span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&gt;Administrator &lt;span class=&quot;count&quot;&gt;\\((\\d+)\\)&lt;/gi</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&gt;Administrator &lt;span class=<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>count\\<span style="color: #3366CC;">&quot;&gt;(&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&lt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>txt<span style="color: #339933;">;</span>
		    <span style="color: #009900;">&#125;</span>
&nbsp;
		    <span style="color: #003366; font-weight: bold;">var</span> n<span style="color: #339933;">=</span><span style="color: #009966; font-style: italic;">/&gt;All &lt;span class=&quot;count&quot;&gt;\\((\\d+)\\)&lt;/gi</span>.<span style="color: #660066;">e</span><span style="color: #339933;">-</span>xec<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> txt<span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&gt;All &lt;span class=&quot;count&quot;&gt;\\((\\d+)\\)&lt;/gi</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&gt;All &lt;span class=<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>count\\<span style="color: #3366CC;">&quot;&gt;(&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&lt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>txt<span style="color: #339933;">;</span>
		    <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>		
          <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    addLoadEvent<span style="color: #009900;">&#40;</span>setUserName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Note the three periods on the first line? Ingeniously, when you look at the records directly in the database using a tool like <a href="http://www.phpmyadmin.net/">phpMyAdmin</a>, it defaults to only showing the first line. This makes the field appear as &#8216;&#8230;&#8217; &#8211; you only see the actual evil payload if you activate the full text view.</p>
<p>WordPress spits out the above code verbatim on the users page, without turning it into HTML entities. When run, the code looks for the maliciously injected users in the HTML, hides them and modifies the administrators count. This means that as long as Javascript is turned on in your browser, you&#8217;ll never see the extra users without manually looking into the database.</p>
<p>This was my users page with Javascript turned on:<br />
<img src="http://danforys.com/wp-content/uploads/2009/11/json.png" alt="json" title="json" width="600" height="125" class="alignnone size-full wp-image-126" /></p>
<p>and this is what happened when I turned Javascript off:<br />
<img src="http://danforys.com/wp-content/uploads/2009/11/jsoff.png" alt="jsoff" title="jsoff" width="600" height="229" class="alignnone size-full wp-image-127" /></p>
<p>Magic! Three extra users have appeared! </p>
<p>Again, note the three periods that appear as the user name &#8211; this is where the sneaky Javascript was embedded. The other sneaky thing is that two out of the three injected users had managed to replicate my correct email address, the third had it set as &#8216;www@www.com&#8217;.</p>
<p>Once a malicious user is logged in as an adminstrator, they have free reign to do as they please &#8211; including directly modifying PHP code in the WordPress admin panel. It turns out that the server 500 errors were due to the hackers modifying the theme files I was using. It took a clean install of the theme before I could get into the blog again. Hopefully, I&#8217;ll do a post-mortem on the theme and post any malicious findings here.</p>
<p>The moral of the story? Keep your WordPress install up to date and keep an eye on the users table.</p>
<p>If you think you&#8217;ve been hacked, I&#8217;ve spotted a couple of <a href="http://ocaoimh.ie/did-your-wordpress-site-get-hacked/">useful</a> <a href="http://smackdown.blogsblogsblogs.com/2008/06/24/how-to-completely-clean-your-hacked-wordpress-installation/">guides</a> to dealing with the aftermath.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/symptoms-of-a-wordpress-hack/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part 3: Array basics</title>
		<link>http://dan.forys.co.uk/learning-php-part-3-array-basics/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-3-array-basics/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 22:38:10 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=84</guid>
		<description><![CDATA[In part 2, you were introduced to the concept of variables and the string, integer and floating point types. In part 3 we&#8217;ll go through a more advanced type; the array. What is an array? An array is a list of values. Each value has a unique name which is called its key. Imagine you [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.danforys.com/2009/10/25/learning-php-part-2-variable-basics/">part 2</a>, you were introduced to the concept of variables and the string, integer and floating point types. In part 3 we&#8217;ll go through a more advanced type; the array.</p>
<p><span id="more-84"></span></p>
<h3>What is an array?</h3>
<blockquote><p>An array is a list of values. Each value has a unique name which is called its <strong>key</strong>.</p></blockquote>
<p>Imagine you wanted to store a list of colours: Red, Blue and Yellow. You can store this in an array as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$colours</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Red'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here, you assigned the array of values to a variable called &#8220;$colours&#8221;. Each item in this array (called an element) is a string (remember strings are enclosed by single or double quotes). Note that each item in the array is separated by a comma, and the whole list is enclosed in brackets.</p>
<h3>Outputting an array</h3>
<p>This is all very nice, but how do we get the items out of the array? You can&#8217;t simply echo out an entire array. This is where PHP has some very powerful functions. One of the most common, is the foreach:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$colours</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</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>This is the first example of a PHP loop. Foreach goes through each item in an array and lets you perform operations on it. Here, we are telling it to operate on the $colours array we defined above. In turn, each item in the array is assigned to a second variable called $item. The two curly braces &#8216;{&#8216; and &#8216;}&#8217; enclose a set of instructions to run on each element; in this case echoing out the item followed by an HTML break. This gives:</p>
<pre>
Red
Blue
Yellow
</pre>
<blockquote><p>Note that foreach always operates on the elements in the same order that they were put into the array.</p></blockquote>
<p>Another, slightly more complicated example. This time, we&#8217;ll use an array of integers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$numberArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numberArray</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$number</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this adds $number to the current value of $total</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The total is '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$total</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, after creating the array, we have also created a variable called $total, setting its value to 0. Using foreach, we then loop through each element of the $numberArray and add it to the $total. Finally, we echo out the total, giving this result:</p>
<pre>The total is 12</pre>
<h3>Mixed arrays</h3>
<p>Arrays needn&#8217;t be just lists of a single type; you can mix and match as you like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mixedArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Fred'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">23</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Cheesy nibbles&quot;</span><span style="color: #339933;">,</span> <span style="color:#800080;">19.6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Each element in the array has its own type, just as when you assign values to variables. This becomes very useful when we deal with more advanced topics.</p>
<h3>Array keys</h3>
<p>At the start, we mentioned that each item in an array has a key, as well as a value:</p>
<blockquote><p>If you don&#8217;t explicitly supply a key, PHP will automatically number your elements, starting from 0</p></blockquote>
<p>Remember the colours array from the start of this post? This means that when we created the array, Red had the key 0, Blue had a key of 1 and Yellow was 2. The keys give you a way of referring to specific elements in the array. One way of doing this is with square brackets:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$colours</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Red'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$colours</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'Blue' - numbering starts from 0!</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$colours</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'Yellow'</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$colours</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// not defined, may show an error depending on configuration</span></pre></div></div>

<p>You can use the same syntax to define an element with a specific key:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$colours</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Purple'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now the array has a new item &#8216;Purple&#8217; with a key of 7. Now there are four items in the array: Red (0), Blue (1), Yellow (2), Purple (7). Note that the keys don&#8217;t need to be consecutive.</p>
<p>You can also specify the array keys when you initially create the array, using PHPs &#8216;=>&#8217; notation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$colours</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Purple'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$colours</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</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>Remember that foreach loops through the items in the order they were inserted; regardless of its key. This means the above code outputs:</p>
<pre>Red
Purple
Blue
Yellow</pre>
<p>You can also use the keys in a foreach loop:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$colours</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Purple'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$colours</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Key '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' has value '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</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>Now, each time a $item is fetched from $colours, its corresponding key is assigned to the variable $key. It uses the same &#8216;=>&#8217; notation as above. This results in:</p>
<pre>Key 0 has value Red
Key 7 has value Purple
Key 1 has value Blue
Key 2 has value Yellow</pre>
<h3>Associative arrays</h3>
<p>Just as array items can be different types, so can their keys. This is an immensely useful feature of PHP that you will refer to again and again. Arrays with non-numeric keys are called associative arrays.</p>
<p>You define an associative array in exactly the same way as any other array. Here, we&#8217;ll define an array of details about a person:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$person</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Fred'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dislikes'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Mushrooms);
echo $person['</span>age<span style="color: #0000ff;">']; // outputs 25</span></pre></div></div>

<p>You can use square brackets to assign associative values as well. This example is equivalent to the previous:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$person</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Fred'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dislikes'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Mushrooms'</span><span style="color: #339933;">;</span></pre></div></div>

<h3>To conclude</h3>
<p>Arrays are one of the most useful types in PHP; so feel free to play around with the examples above until you feel confident with them.</p>
<p>Now you&#8217;ve been through the basics of variables and arrays, we can go on to build your first dynamic web page in part 4.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-3-array-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning PHP â€“ Part 2: Variable Basics</title>
		<link>http://dan.forys.co.uk/learning-php-part-2-variable-basics/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-2-variable-basics/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 19:53:24 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=72</guid>
		<description><![CDATA[After part 1, you should be familiar with the echo statement, which outputs a string to the browser: echo 'This is a string'; (Note that from now on the opening (&#60;?php) and closing (?&#62;) PHP tags are omitted from examples, but should still be included in any source code you create) On its own, it [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://www.danforys.com/2009/10/24/learning-php-part-1-introduction/">part 1</a>, you should be familiar with the echo statement, which outputs a string to the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Note that from now on the opening (&lt;?php) and closing (?&gt;) PHP tags are omitted from examples, but should still be included in any source code you create)</p>
<p>On its own, it does not appear to be useful; after all we could have just written &#8216;This is a string&#8217; in HTML for the same effect. The real power comes from using PHP variables.<br />
<span id="more-72"></span></p>
<blockquote><p>
Think of a variable as a named box, in which you can place something. Later, you can retrieve the thing you put in the box, you just look it up by its name.</p></blockquote>
<h3>Assigning values to variables</h3>
<p>Creating a variable is as easy as giving it a name and telling it what value you want it to store. In PHP, variables always begin with a dollar sign ($)</p>
<blockquote><p>
A variable name must begin with a letter, or an underscore. The rest of the name can be any combination of letters, numbers and/or underscores.
</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This creates a new variable with the name of &#8216;$string&#8217; and stores the value &#8216;This is a string&#8217; inside it. You can combine the echo statement with a variable to output the contents of a variable to the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will output &#8216;This is a string&#8217; as with the first example.</p>
<p>Any subsequent values you set will overwrite the previously stored value:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This overwrites the previous value'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'This overwrites the previous value'</span></pre></div></div>

<p>Here we&#8217;ve introduced a style of PHP comment. Comments are totally ignored by PHP making them useful for documenting your code. I&#8217;ve used the &#8216;//&#8217; comment, where PHP will ignore everything up to the end of the line.</p>
<h3>Variable types</h3>
<p>In PHP, variables can be one of several types. These types affect what operations you can perform on the variable and how the values behave. Three basic variable types are listed below.</p>
<h4>Strings</h4>
<p>Strings are collections of characters. You denote strings by enclosing them in single or double quotes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a single quoted string'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a double quoted string&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are some differences with how single and double quoted strings are interpreted &#8211; we&#8217;ll talk about that later.</p>
<p>You can add (concatenate in PHP-speak) two strings together using a period &#8216;.&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This string '</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'is concatenated from two parts'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'This string is concatenated from two parts';</span></pre></div></div>

<p>You can also do this with two variables containing strings:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This string '</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'is concatenated from two parts'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string1</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$string2</span> <span style="color: #666666; font-style: italic;">// same result as above</span></pre></div></div>

<h4>Integers</h4>
<p>Integers are whole numbers. A whole number is one without a decimal point.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$integer</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 42</span></pre></div></div>

<p>You can perform mathematical operations on integers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$integer1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$integer2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer1</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$integer2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 30</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$integer2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 70</span></pre></div></div>

<h4>Floating point numbers</h4>
<p>Floating point numbers are numbers with a decimal point. In practice, they work similarly to integers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$float1</span> <span style="color: #339933;">=</span> <span style="color:#800080;">10.5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$float2</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.2</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$float1</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$float2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 10.3</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$float1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$float2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 10.7</span></pre></div></div>

<p>Note that floating point numbers have a very important gotcha:</p>
<blockquote><p>
Inside the computer memory, floating point numbers are not stored as exact representations. This means rounding errors can occur. For this reason, never use floating point numbers where accuracy is important; such as in financial transactions for example.
</p></blockquote>
<h3>Other types</h3>
<p>There are several other types of variable in PHP. The other types are slightly more advanced and will be introduced later as we need them.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-2-variable-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Learning PHP &#8211; Part 1: Introduction</title>
		<link>http://dan.forys.co.uk/learning-php-part-1-introduction/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-1-introduction/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 21:13:26 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=48</guid>
		<description><![CDATA[So, you&#8217;ve got to grips with the basics of HTML and CSS, and now you want to actually make your web page do something? Well, PHP is a great way to do that. In this series of blog posts, I will introduce the PHP language and teach many useful techniques that are used in professional [...]]]></description>
			<content:encoded><![CDATA[<p>So, you&#8217;ve got to grips with the basics of <abbr title="HyperText Markup Language">HTML</abbr> and <abbr title="Cascading Style Sheets">CSS</abbr>, and now you want to actually make your web page do something? Well, <a href="http://www.php.net">PHP</a> is a great way to do that. In this series of blog posts, I will introduce the PHP language and teach many useful techniques that are used in professional PHP development.</p>
<p><span id="more-48"></span></p>
<h3>What is PHP?</h3>
<p>PHP is a general purpose scripting language. At its most basic level, it can be used to embed logic into web pages. If HTML is the meaning and CSS is the presentation, PHP is the engine that makes it work. As you will see later in the series, it also has powerful object oriented abilities that help you produce reusable and easily maintainable code. For a detailed history of PHP, see the excellent <a href="http://en.wikipedia.org/wiki/Php">Wikipedia entry</a>.</p>
<h3>What you need to get started</h3>
<p>First, you&#8217;ll need a web server. You won&#8217;t get very far unless you can actually see the web pages you create. Personally, I prefer using <a href="http://httpd.apache.org/">Apache</a> &#8211; and indeed, this guide assumes you are using Apache.</p>
<p>There are several ways to get Apache up and running, for Windows users there is <a href="http://www.wampserver.com/en/">WAMP</a> or <a href="http://www.easyphp.org/">EasyPHP</a>. For Mac users, OS X has <a href="http://www.youtube.com/watch?v=ep5E4Y4N65E">Apache built in</a>, or you can use a self-contained environment such as <a href="http://www.mamp.info/en/index.html">MAMP</a>.</p>
<p>My preferred platform for the web is Linux &#8211; as far as I&#8217;m concerned, Linux is the <abbr title="Operating System">OS</abbr> of the web. Whilst Windows is an excellent general purpose OS, it has some major differences with Linux that can catch out the unwary. It&#8217;s dead easy to either get a Linux-based web host, or set up a virtual machine on Windows.</p>
<p>Finally, you need an editor. Some purists prefer to use simple text editors, the best of which give you syntax highlighting to make your life easier. Personally, I use <a href="http://www.netbeans.org/">NetBeans</a>. Handily, NetBeans is available for the Mac, Windows and Linux. I prefer NetBeans because it automatically highlights any PHP errors as you type &#8211; an invaluable feature. It also offers some useful advanced features that will appear in this blog later.</p>
<h3>First steps</h3>
<p>By default, Apache will execute PHP embedded into any file with a .php extension. Create a new file called, for example, my-first.php and enter the following:</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: #b1b100;">echo</span> <span style="color: #0000ff;">'My first PHP file!'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The &#8216;&lt;?php&#8217; and &#8216;?&gt;&#8217; are the opening and closing tags respectively. By default, PHP uses an XML style syntax to denote where your PHP starts and stops. Anything outside these tags is sent to the browser verbatim. You could have HTML markup outside these tags for example.</p>
<p>Anything within the tags is intercepted by the PHP engine before the page is sent to the browser. In this case, the &#8216;echo&#8217; causes the content inbetween the single quotes to be sent to the browser. Note the semicolon at the end of the line &#8211; PHP uses the semicolon to signify the end of the line. It&#8217;s a very common mistake to leave out the semicolon, which is why using a good editor like netbeans will highlight the mistake for you.</p>
<p>To run your first PHP script, you&#8217;ll need to put it on your server. This will mean copying it to your document root, or uploading it using FTP. Once the file is in place, you should be able to run it by going to <a href="http://localhost/my-first.php">http://localhost/my-first.php</a> if it is on your local machine, or http://whatever-your-host-is.com/my-first.php if you&#8217;re hosting it somewhere remotely.</p>
<p>If all went to plan, you should see a blank page with the text &#8216;My first PHP file!&#8217; on it. It&#8217;s not much, but an important first step into the world of the dynamic web.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-1-introduction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unsetting http headers in PHP</title>
		<link>http://dan.forys.co.uk/unsetting-http-headers-in-php/</link>
		<comments>http://dan.forys.co.uk/unsetting-http-headers-in-php/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 15:28:03 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[ie7]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=40</guid>
		<description><![CDATA[I just came across a subtle issue affecting Internet Explorer users (well, fancy that!) and HTTPS connections. One of my clients has a site that downloads a series of results as a CSV file, which they open in Excel. Unfortunately, Internet Explorer was refusing to download the file, and was presenting an error message reading [...]]]></description>
			<content:encoded><![CDATA[<p>I just came across a subtle issue affecting Internet Explorer users (well, fancy that!) and HTTPS connections.</p>
<p>One of my clients has a site that downloads a series of results as a CSV file, which they open in Excel. Unfortunately, Internet Explorer was refusing to download the file, and was presenting an error message reading &#8220;Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.&#8221;</p>
<p>To add to my confusion, this was happening on the live server (PHP4), but not on my dev server (PHP5) <em>which both use the same code</em>.</p>
<p>In the end, I happened upon a Microsoft Knowledge Base article that <a href="http://support.microsoft.com/kb/316431">explained the problem</a>. Basically, IE obeys any &#8220;no-cache&#8221; headers you send to the browser. Without caching the file, Office applications cannot open the file when served over HTTPS.</p>
<p>How to solve the issue? Remove the cache header(s) &#8211; but how?</p>
<p>The <a href="http://uk2.php.net/manual/en/function.header.php">PHP manual</a> doesn&#8217;t make it entirely clear, and I found the solution by accident. To remove a header, use the same syntax as for setting a header, but only include a space after the colon.</p>
<p>For example, for the &#8220;Pragma&#8221; header:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pragma: '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note:</strong> You must include the space after the &#8220;:&#8221; or the header will not be unset.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/unsetting-http-headers-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Intermittent 1px gap in Firefox 3</title>
		<link>http://dan.forys.co.uk/intermittent-1px-gap-in-firefox-3/</link>
		<comments>http://dan.forys.co.uk/intermittent-1px-gap-in-firefox-3/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 12:36:56 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[firefox]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=33</guid>
		<description><![CDATA[My work has just pushed out an update, upgrading our users to Firefox 3. Naturally, we&#8217;ve been busy looking through our internally produced sites to check all is fine in the new version. In 99% of cases, all our sites look the same in Firefox 3, apart from one notable exception. In this one case, [...]]]></description>
			<content:encoded><![CDATA[<p>My work has just pushed out an update, upgrading our users to Firefox 3. Naturally, we&#8217;ve been busy looking through our internally produced sites to check all is fine in the new version.</p>
<p>In 99% of cases, all our sites look the same in Firefox 3, apart from one notable exception. In this one case, there is a 1px gap between the left-hand margin (which is centred using &#8220;margin-left: auto&#8221;) and the header image. Being the perfectionists we are, we don&#8217;t want a 1px white gap in our nice header.</p>
<p>Oddly, this 1px gap would come and go as the browser window is horizontally resized. This behaviour set alarm bells ringing that it&#8217;s some kind of rounding error in the &#8220;auto&#8221; positioning vs the exact pixel dimensions of our centred container.</p>
<p>After a bit of searching, we found an entry in <a href="http://ejohn.org/blog/sub-pixel-problems-in-css/" target="_blank">John Resig&#8217;s blog</a> (of <a href="http://jquery.com/" target="_blank">JQuery</a> fame), where he has stumbled upon the issue and developed a test case for the main browsers. Turns out that fixing it is a challenge, as all the browsers round the numbers differently. Problem is, there&#8217;s no standard for how the browsers should round the pixels, so fixing it in one browser will almost certainly break it in another.</p>
<p>Ho hum, here&#8217;s hoping for a future standard.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/intermittent-1px-gap-in-firefox-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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