<?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>Chris Clarke&#039;s Blog &#187; PHP</title>
	<atom:link href="http://sorrowfulunfounded.com/category/code/php-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://sorrowfulunfounded.com</link>
	<description>Official Weblog of Christopher Clarke</description>
	<lastBuildDate>Thu, 10 Nov 2011 04:32:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>WordPress eBook Export</title>
		<link>http://sorrowfulunfounded.com/blog/2010/01/28/wordpress-ebook-export/</link>
		<comments>http://sorrowfulunfounded.com/blog/2010/01/28/wordpress-ebook-export/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 06:44:51 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=557</guid>
		<description><![CDATA[This is a very very early development release of a plugin for WordPress to export a category as an eBook, currently only ePub. Some features don&#8217;t work and many are not implemented. Anyway, use at your own risk. Download Plugin Git repository.]]></description>
			<content:encoded><![CDATA[<p>This is a very very early development release of a plugin for WordPress to export a category as an eBook, currently only ePub. Some features don&#8217;t work and many are not implemented.</p>
<p>Anyway, use at your own risk.<br />
<a href="http://upload.sorrowfulunfounded.com/misc/wp_ebook_export.zip">Download Plugin</a></p>
<p><a href="http://github.com/chrisclarke/eBook-Export-Plugin-for-WordPress">Git repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2010/01/28/wordpress-ebook-export/feed/</wfw:commentRss>
		<slash:comments>320</slash:comments>
		</item>
		<item>
		<title>Simple Cache Library for CodeIgniter</title>
		<link>http://sorrowfulunfounded.com/blog/2010/01/24/simple-cache-library-for-codeigniter/</link>
		<comments>http://sorrowfulunfounded.com/blog/2010/01/24/simple-cache-library-for-codeigniter/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 08:13:53 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=552</guid>
		<description><![CDATA[Note: This library has only been tested with the 1.7 branch of CodeIgniter. It has not been tested with CodeIgniter 2.0. I wrote a simple caching library for CodeIgniter since database caching and page output caching were not suitable for my needs. Usage: class Test extends Controller &#123; &#160; function main&#40;&#41; &#123; &#160; // load [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note:</strong> This library has only been tested with the 1.7 branch of CodeIgniter. It has not been tested with CodeIgniter 2.0.</p>
<p>I wrote a simple caching library for <a href="http://codeigniter.com">CodeIgniter</a> since database caching and page output caching were not suitable for my needs.</p>
<p><strong>Usage:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// load the library</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'simple_cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// key is the name you have given to the cached data</span>
		<span style="color: #666666; font-style: italic;">// will check if the item is cached</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">simple_cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_cached</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// not cached, do our things that need caching</span>
			<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'print'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Hello World'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// store in cache</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">simple_cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_item</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">simple_cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_item</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I don&#8217;t think I need to explain anymore? Maybe I will update with a better description later.</p>
<p>I&#8217;m releasing it under the GPLv3. Enjoy.</p>
<p><a href="http://upload.sorrowfulunfounded.com/misc/simple-cache.7z">Download simple-cache.7z</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2010/01/24/simple-cache-library-for-codeigniter/feed/</wfw:commentRss>
		<slash:comments>671</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Installing PHP (Windows)</title>
		<link>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 09:11:16 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=428</guid>
		<description><![CDATA[This tutorial will take you through the steps needed to install PHP on Microsoft Windows. I&#8217;m using Vista, but with minor changes, these instructions should also be valid for Windows XP and Windows 7 as well. I will cover Linux and Mac OS X in another, future post. Instead of going through and telling you [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will take you through the steps needed to install <a href="http://php.net">PHP</a> on Microsoft Windows. I&#8217;m using Vista, but with minor changes, these instructions should also be valid for Windows XP and Windows 7 as well.<span id="more-428"></span> I will cover Linux and Mac OS X in another, future post.</p>
<p>Instead of going through and telling you to download Apache, PHP, phpMyAdmin and MySQL individually, I’m going to instruct you to go and download a software package called <a href="http://www.wampserver.com/en/download.php">WampServer</a>. You will need to download the installer from the <a href="http://www.wampserver.com/en/download.php">WampServer</a> webpage. WAMP stands for Windows, Apache, MySQL and PHP meaning the software will install all these at once. This software will install like a normal Windows program, it is safe to just click Next until you reach the end of the installer. The installer will place a shortcut in your Start Menu and on your Desktop called Start WampServer. To start your PHP-enabled web server, all you need to do is double click this icon (in Vista, User Account Control will prompt you, it is safe to click Allow).</p>
<p>To check that your server is working, point your web browser to <a href="http://localhost/">http://localhost/</a>. If you see a page with server information, your server is working. If you don’t, post in the comments and I will help you troubleshoot.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="wampserverindex" src="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampserverindex.jpg" border="0" alt="wampserverindex" width="423" height="234" /></p>
<p>WampServer will add a new icon to your taskbar’s notification area which you can left click and control the various aspects of your new server.</p>
<p><a href="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampleftclick.jpg" rel="lightbox[428]"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="wampleftclick" src="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampleftclick_thumb.jpg" border="0" alt="wampleftclick" width="346" height="269" /></a></p>
<p>You can use the various options in this menu to configure your server. You will probably be most interested in the PHP submenu where you can easily enable and disable PHP extensions and features without having to deal with configuration files. The option to make changes to PHP using php.ini is still there if you want it though, and it won’t break the ability of WAMP to automatically make changes using the menu.</p>
<p>If you went with the default configuration in the installer, the folder in which you place your files will be located in the following location:</p>
<p>C:\wamp\www</p>
<p>Files and Folders that you place in this folder will be accessible at the following URL:</p>
<p><a href="http://localhost/">http://localhost/</a></p>
<p>It is important to note that this location will only be available on your computer. You can’t go giving this URL to your friends. It isn’t complicated to allow that to happen (with a different URL specific to your PC), but it is beyond the scope of the article.</p>
<p>Congratulations, by just downloading one installer, you now have a fully functional local development server which you can use as a sandbox to learn PHP.</p>
<p>If there is significant interest in a manual installation and configuration process of the individual programs, I will also post a tutorial of that method. In most cases though, for development purposes on Windows, you won’t need to do this.  I should note that I personally use WAMP to quickly get myself setup with a server for development.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/feed/</wfw:commentRss>
		<slash:comments>858</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Hello World + MySQL</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 04:57:32 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=403</guid>
		<description><![CDATA[This tutorial builds on the previous tutorial &#8211; Introduction to PHP: Hello World and as such, you should take that tutorial first if you have not already. In this tutorial, we are going to take our previous Hello World script, and store the date in a database. We will then add the last access time [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial builds on the previous tutorial &#8211; Introduction to <a href="http://sorrowfulunfounded.com/2009/06/introduction-to-php-hello-world/">PHP: Hello World</a> and as such, you should take that tutorial first if you have not already.</p>
<p>In this tutorial, we are going to take our previous Hello World script, and store the date in a database. We will then add the last access time to our original script.<span id="more-403"></span> We are going to use <a href="http://mysql.com">MySQL</a> in this tutorial. I will assume that you already have MySQL and <a href="http://phpmyadmin.net">phpMyAdmin</a> (or equivalent) installed.</p>
<p>The state of our script at the end of the last tutorial was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Our database table will consist of two fields, `ID`, and `current_timestamp`. Both will be of type, integer (INT). You should make `ID` a primary key, and auto_increment. `current_timestamp` can be left with the default options.</p>
<p>For your convenience, I will provide a SQL query to create the table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`last_access`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`ID`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`current_timestamp`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> InnoDB</pre></td></tr></table></div>

<p>You can run the above query in the SQL section of phpMyAdmin when viewing your database.</p>
<p>To connect to our database, we use two functions, <a href="http://au.php.net/mysql_connect">mysql_connect</a>, and <a href="http://au.php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a>.</p>
<p>The first function, mysql_connect takes three parameters or arguments. These parameters are the host name (most of the time, localhost), and a username and password. These should have been provided by your web host. If you installed a server on your own computer, it is likely that username is root, with no password. We are going to place the mysql_connect function in a variable called $connection (be sure to change the parameters to suite your own specific situation):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, we need to use mysql_select_db to access our specific database. I&#8217;ve called mine date, but you may have called it something different. If you have, change as appropriate. mysql_select_db takes two parameters. These are the $connection variable we just set and the name of your database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can go ahead and place these functions at the top of our Hello World script.</p>
<p>Next, we are going to use the <a href="http://us.php.net/mysql_query">mysql_query</a> function to insert the current time into our new database table. mysql_query only takes one parameter and this is an SQL statement. SQL stands for structured query language and is how we interact with our database within our applications. I won&#8217;t go into detail about SQL at this time, but for now, <a href="http://www.w3schools.com">W3Schools</a> offers <a href="http://www.w3schools.com/sql/default.asp">a decent introduction</a>.</p>
<p>Within our query, we will also use PHP&#8217;s time function. This function takes no parameters. It returns the current UNIX time stamp which is the number of seconds since the start of 1970. </p>
<p>Place the following in your script after the connection functions, and run it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, we are going to use the mysql_query function again to retrieve the last access time. This time we will place the mysql_query function into a variable, so we can access the results.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, we want to be able to retrieve the data our SELECT query is retrieving. We can do this with, mysql_fetch_assoc. It takes one parameter, and this is our query variable which we named $result. Like mysql_query, we will also place this into a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Place the SELECT query before the INSERT query but after the connection information.</p>
<p>Now, all the pieces are in place to put our final script together.</p>
<p>Your script should look like this at this point:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now, we are going to add the current hour, minute and second to our date function in our echo language construct. The letters for these are h (hour), i (i since m is reserved for month, the current minute) and s (seconds).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, we want to display the last access date. To do this, we are going to add another echo language construct. Since row only returned one piece of data due to LIMIT 1, we can use it just like we would an array (we will discuss arrays in a later tutorial). For now, all you need to know is that they are used like variables but can store more then one piece of information.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' This script was last accessed at  '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current_timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You will notice this date function is taking a second parameter. This changes the output of date from the current time/date to the time and date which it was when the timestamp was recorded. </p>
<p>Our final script looks 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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' This script was last accessed at  '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current_timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So that concludes this tutorial. As usual, comments, suggestions and most importantly, questions, are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/feed/</wfw:commentRss>
		<slash:comments>713</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Hello World</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 16:35:41 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=380</guid>
		<description><![CDATA[This tutorial will introduce you to PHP with the customary Hello World script. You will be introduced to the echo language construct and the date() function. The goal of this tutorial is to show you how to output text, and display the current date. I will not explicitly say save this as &#8220;file.php&#8221;, upload to [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will introduce you to PHP with the customary Hello World script. You will be introduced to the <a href="http://au.php.net/echo">echo</a> language construct and the <a href="http://au.php.net/manual/en/function.date.php">date()</a> function. The goal of this tutorial is to show you how to output text, and display the current date.<br />
<span id="more-380"></span><br />
I will not explicitly say save this as &#8220;file.php&#8221;, upload to your server, and access in your web browser, but it is strongly encouraged that you do so at each step.</p>
<p>To start, let&#8217;s output the text &#8220;Hello World&#8221; to the screen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Any text placed within the quotes will be outputted to your screen when you access the script in your web browser.</p>
<p>Now, we are going to display the current date in addition to our greeting. We can get the current date using the date function which in this example takes one parameter, but usually takes two. We will discuss that in a future tutorial, but the other parameter is a standard <a href="http://en.wikipedia.org/wiki/Unix_time">Unix timestamp</a> &#8211; the number of seconds since 00:00:00 UTC on January 1, 1970. We do not need to specify it here since the date function will assume I am after today&#8217;s date by default.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The above will output the current date to the screen. The one parameter taken is what defines the format in which our date is returned. The page for <a href="http://php.net/date">date()</a> in the PHP manual has all you need to know about the various ways in which you can format the date.</p>
<p>Now that we have learned how to output some text of our choice, and retrieve today&#8217;s date, let&#8217;s combine the two.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You should notice the backslash preceding the single quote in &#8220;Today&#8217;s&#8221;. This is called escaping and tells PHP to ignore the character and continue on. It will be discussed in much more detail in a tutorial later in this series.</p>
<p><strong>NOTE:</strong> These tutorials may seem like they are assuming too much knowledge at this point, but you will eventually see that this is by design, and that anything not covered and assumed will be covered in tutorials yet to be written or published. The goal is to have a series of related and dependant tutorials, but at this point I am not writing them in order. The <a href="http://sorrowfulunfounded.com/tutorials/">tutorial page</a> will always have the tutorials listed in the intended reading order.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/feed/</wfw:commentRss>
		<slash:comments>343</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Includes</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 12:24:29 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[include_once]]></category>
		<category><![CDATA[incude]]></category>
		<category><![CDATA[require]]></category>
		<category><![CDATA[require_once]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=358</guid>
		<description><![CDATA[This tutorial will introduce you to the PHP include statement, and its almost identical sibling, require. The include statement is used to insert the contents of one file into the contents of another. Usage: 1 2 3 &#60;?php include &#34;/path/to/file.php&#34;; ?&#62; The included file is essentially merged with the file in which the include takes place, [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will introduce you to the <a href="http://au2.php.net/manual/en/function.include.php">PHP include</a> statement, and its almost identical sibling, require.</p>
<p>The include statement is used to insert the contents of one file into the contents of another.<br />
<span id="more-358"></span><br />
<strong>Usage:</strong></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: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The included file is essentially merged with the file in which the include takes place, and the result in effect is one file containing the contents of both.</p>
<p>For example, we have two files. One is called &#8220;functions.php&#8221;, and the other, &#8220;display.php&#8221;. We are going to include &#8220;functions.php&#8221; in to &#8220;display.php&#8221;.</p>
<p>The contents of each script will be as follows.</p>
<p><strong>functions.php:</strong></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;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>display.php:</strong></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;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'functions.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Essentially, what we are saying is that the contents of display.php should actually be:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* including functions.php */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* finished including functions.php */</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and the output is as so:</p>
<blockquote><p>Today&#8217;s Date is 11/06/09.</p></blockquote>
<p>You will notice that the PHP opening and closing tags (&lt;?php and ?&gt;) were maintained.PHP automatically closes its tags before and opens them after the inclusion has taken place. This is because PHP does not assume that the file included only contained PHP, for example it may only contain HTML or CSS. </p>
<p>The code within &#8220;functions.php&#8221; was executed just as if it had been in the PHP file containing the include statement in the first place.  &#8220;display.php&#8221; has access to any methods/functions, variables and other things defined in &#8220;functions.php&#8221;, and &#8220;functions.php&#8221; has access to any of those which are specified in &#8220;display.php&#8221; or other files it might include prior to &#8220;functions.php&#8221; inclusion.</p>
<p><strong>How does the require statement differ from the include statement?</strong></p>
<p>With include, if the file does not exist, your PHP script will not halt, and will continue to execute until it reaches the end. In the case of require, the script will cease execution and a fatal error will occur if the file cannot be found. You should use require to include any file you absolutely depend on like a &#8220;config.php&#8221; file containing your database access information.</p>
<p>In usage, require only differs in the text used. It is otherwise identical to include.</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: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>What about require_once and include_once? How are they different?</strong></p>
<p>Appending _once to either require or include will only allow the file to be included once. Future include statements referencing said file will not get included again.</p>
<p>That is the conclusion of my first tutorial &#8211; an introduction to PHP includes. If you have any questions, corrections, suggestions for tutorials, or general comments, please feel free to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/feed/</wfw:commentRss>
		<slash:comments>158</slash:comments>
		</item>
	</channel>
</rss>

