<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss version="2.0">
	<channel>
		<title>Total PHP RSS Feed</title>
		<description>The latest articles published on Total PHP</description>
		<link>http://www.total-php.com/</link>
		<pubDate>Tue, 14 Jul 2009 1:18:46 +0000</pubDate>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/TotalPHPRssFeed" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">TotalPHPRssFeed</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
			<title>How to Read an RSS Feed with PHP 5</title>
			<description>&lt;p&gt;PHP 5's ability to read XML files is fantastically easy to use. In the past it was possible but it required quite a bit of long winded code to get any where. PHP 5's &lt;strong&gt;SimpleXmlElement&lt;/strong&gt; function makes working with XML a breeze, and with much less code too! Let's take the following example:&lt;/p&gt;

&lt;p class="code"&gt;
$feed = file_get_contents('http://www.mywebsite.com/rss/');&lt;br /&gt; 
$rss = new SimpleXmlElement($feed);&lt;br /&gt;
&lt;br /&gt;
foreach($rss-&gt;channel-&gt;item as $entry) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;echo "&amp;lt;p&amp;gt;&amp;lt;a href='$entry-&gt;link' title='$entry-&gt;title'&amp;gt;" . $entry-&gt;title . "&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;";&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;In the above example we want to display the contents of an RSS feed, in our example &lt;strong&gt;http://www.mywebsite.com/rss/&lt;/strong&gt;. Using the &lt;strong&gt;file_get_contents&lt;/strong&gt; function we can get the contents of this RSS file and put them in a variable called &lt;strong&gt;$feed&lt;/strong&gt;. We then use the &lt;strong&gt;SimpleXmlElement&lt;/strong&gt; function to process the RSS feed and put it in a format we can use. This usable format (now an objext) is stored in &lt;strong&gt;$rss&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;$rss&lt;/strong&gt; object can now be used to get any information that was in our RSS feed. The foreach loop does just that, looping through each &lt;strong&gt;item&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It really is that simple, and there's loads more you can do with the &lt;strong&gt;SimpleXmlElement&lt;/strong&gt; class - check out the &lt;a href="http://uk3.php.net/manual/en/book.simplexml.php" class="blank"&gt;PHP documentation&lt;/a&gt; for more.&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/19/how-to-read-an-rss-feed-with-php-5/</link>
			<pubDate>Wed, 22 Apr 2009 6:48:33 +0000/</pubDate>
		</item>
		<item>
			<title>Performing searches on strings using strpos</title>
			<description>&lt;p&gt;There are occasions where you might want to &lt;strong&gt;search for a phrase within a string&lt;/strong&gt; in PHP. Using the PHP function strpos we can do just this nice and easily.&lt;/p&gt;

&lt;p&gt;It could be that you want to check to see if something exists within a string, maybe as part of some validation or as a conditional action. PHP enables to do this very simply and this can come in handy in all sorts of situations.&lt;/p&gt;

&lt;p&gt;As an example, I want to see if the word "string" appears in a sentence.&lt;/p&gt;

&lt;p class="code"&gt;
$sentence = 'Using strpos we can find out if a string exists in another string';&lt;br /&gt;
&lt;br /&gt;
if ( strpos($sentence, 'string') ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;// yes it does&lt;br /&gt;
} else {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;// no it doesn't&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;Obviously you can populate the sentence variable with database content within a loop or similar during practical applications.&lt;/p&gt;

&lt;p&gt;Also quite handy in PHP 5 only is the &lt;strong&gt;stripos&lt;/strong&gt; function, which works in exactly the same way, but is case insensitive. So as an example the following would still find a result, whereas when using strpos it does not:&lt;/p&gt;

&lt;p class="code"&gt;
$sentence = 'Using stripos we can find out if a String exists even though it has a capital letter in it';&lt;br /&gt;
&lt;br /&gt;
if ( stripos($sentence, 'string') ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;// yes it does&lt;br /&gt;
} else {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;// no it doesn't&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/18/performing-searches-on-strings-using-strpos/</link>
			<pubDate>Wed, 17 Dec 2008 16:37:34 +0000/</pubDate>
		</item>
		<item>
			<title>PHP form validation basics</title>
			<description>&lt;p&gt;This is the start of a couple of articles about &lt;strong&gt;form validation&lt;/strong&gt;, the different kinds and how to validate different types of information. In this first article I am just going to cover the basics to start off with - checking for entry.&lt;/p&gt;

&lt;p&gt;If you can imagine, we have a form (say a contact form) with a number of fields on it (name, email, telephone number, comments etc). We might want each of these fields to be required, so someone filling out the form must fill them out. If they miss a field we want to display a friendly error message to inform them. The best way to do this is have the form and process contained in one PHP script, so the HTML form submits to itself. This way we can easily display any error messages alongside the form. The HTML for the form might look something like this:&lt;/p&gt;

&lt;p class="code"&gt;
&amp;lt;form action="contact.php" method="post"&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Name:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="name" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Email:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="email" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Telephone:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="telephone" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Comments:&amp;lt;br /&amp;gt;&amp;lt;textarea name="comments"&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;input type="submit" name="submit" value="Send" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;
&lt;/p&gt;

&lt;p&gt;The above form when submitted will be send to itself (contact.php). We are using post rather than get so the contact information doesn't clutter up our URL.&lt;/p&gt;

&lt;p&gt;So now at the very top of our script the first thing we need to check for is a form submission. We can do this like so:&lt;/p&gt;

&lt;p class="code"&gt;
if ( $_POST['submit'] ) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;/p&gt;

&lt;p&gt;We can use this to see if the submit button (that has the name submit) has been clicked. If it hasn't it will ignore everything inside the if statement and display a blank form (ie it is the first time someone has come to the page).&lt;/p&gt;

&lt;p&gt;Next we need to start validating the input. First we'll check the name field:&lt;/p&gt;

&lt;p class="code"&gt;
if ( $_POST['submit'] ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = '';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( $_POST['name'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your name';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;Here we have created an 'error' variable. This will store the error message and later display it on screen for the user. Next we use an if statement to see if the name field is blank. If it is we create some error text in the error variable.&lt;/p&gt;

&lt;p&gt;Now we want to continue doing this for each field:&lt;/p&gt;

&lt;p class="code"&gt;
if ( $_POST['submit'] ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = '';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( $_POST['name'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your name';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['email'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your email address';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['telephone'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your telephone number';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['comments'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your comments';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;As you can see above we have added an if statement for each field in the form. We only want it to display the first error message it comes to however, so after checking the first field, we also check to see if the error variable is blank - if it is it means there are no errors previous to checking each field. This is so the error message relates to the first empty field rather than the last.&lt;/p&gt;

&lt;p&gt;If there are no error messages we can go ahead and do whatever process is required, whether it's send an email or store information in a database table:&lt;/p&gt;

&lt;p class="code"&gt;
if ( $_POST['submit'] ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = '';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( $_POST['name'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your name';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['email'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your email address';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['telephone'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your telephone number';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error &amp;&amp; $_POST['comments'] == '' ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$error = 'Error: Please enter your comments';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( !$error ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//form valid, to whatever we need to here.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;This is achieved above with a simple if statement checking to see if there are any errors. If there are some errors it will not go through this if statement, and continue back to the original form.&lt;/p&gt;

&lt;p&gt;If errors are found we want to display them on the page. We also want to prefill the form with the information that is valid so the user doesn't have to enter it all again. So let's edit the form we currently have so it looks something like this:&lt;/p&gt;

&lt;p class="code"&gt;
&amp;lt;?php if ( $error ) { ?&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p class="error"&amp;gt;&amp;lt;?=$error;?&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;?php } ?&amp;gt;&lt;br /&gt;
&amp;lt;form action="contact.php" method="post"&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Name:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="name" value="&amp;lt;?=$_POST['name'];?&amp;gt;" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Email:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="email" value="&amp;lt;?=$_POST['email'];?&amp;gt;" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Telephone:&amp;lt;br /&amp;gt;&amp;lt;input type="text" name="telephone" value="&amp;lt;?=$_POST['telephone'];?&amp;gt;" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;Comments:&amp;lt;br /&amp;gt;&amp;lt;textarea name="comments"&amp;gt;&amp;lt;?=$_POST['comments'];?&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;input type="submit" name="submit" value="Send" /&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;
&lt;/p&gt;

&lt;p&gt;The above code checks to see if an error exists and if it does displays it on the page. We also put the submitted information into each field. If it is the first time someone comes to the form it will still look blank as the post values are empty and there is no error message.&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/17/php-form-validation-basics/</link>
			<pubDate>Mon, 15 Dec 2008 5:00:46 +0000/</pubDate>
		</item>
		<item>
			<title>Using a global configuration file</title>
			<description>&lt;p&gt;It's one of the basics of PHP and if you read any kind of tutorial or book on the subject it's one of the first things they cover, yet I still see people no using a global 'configuration' or 'include' file. &lt;strong&gt;It's one of the easiest things to do and will make your life so much easier!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This 'configuration' file will include anything that needs to be run on every page of the website. This could be database connections, creating classes that are used everywhere on the site, checking for user sessions, initiating a template engine etc. These things that are required for every script in order for them to run correctly, but we don't want to have to include the same code on tens or even hundreds of scripts now do we?&lt;/p&gt;

&lt;p&gt;We can simply put all this in a separate file and then 'include' it on every script at the top, so it's all done and dusted. We can do this using the &lt;strong&gt;include&lt;/strong&gt; function like so:&lt;/p&gt;

&lt;p class="code"&gt;include 'config.php';&lt;/p&gt;

&lt;p&gt;Now the common code will run as if it was on the page itself - nice and easy! Now if there's a bug or we need to update some of the common code we only have to do it once!&lt;/p&gt;

&lt;p&gt;Remember you can do this to create headers and footers or columns that are common across your site too if you use PHP for your templating, and template engines like &lt;a class="blank" href="http://www.smarty.net/"&gt;smarty&lt;/a&gt; have their own include functionality so you can create global templates.&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/16/using-a-global-configuration-file/</link>
			<pubDate>Fri, 05 Dec 2008 3:29:53 +0000/</pubDate>
		</item>
		<item>
			<title>Creating a text or csv file of information from your database</title>
			<description>&lt;p&gt;There are some occasions where you would want to export your site's information in CSV or similar text format. You might want to do this so you can view reports in a spreadsheet, or you might want an export of your product information to upload to a service like Google Products. Either way the method and end result are essentially the same.&lt;/p&gt;

&lt;p&gt;First lets think about what steps need to occur to get our end result. First, we need to get our information from the database. Then we need to put that information in the correct format. Finally we want to save the result as a file with a ".csv" or ".txt" extension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting our information from the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I will presume readers will know how to create the database connection itself so I won't dwell on it here. In our example I am querying a 'products' table from an online store ready to create a product feed.&lt;/p&gt;

&lt;p class="code"&gt;
$db&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= new mysqli('localhost', 'root', 'password', 'database');&lt;br /&gt;
&lt;br /&gt;
$result&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= $db-&gt;query("SELECT * FROM products");&lt;br /&gt;
&lt;br /&gt;
while ( $record = $result-&gt;fetch_assoc() ) {&lt;br /&gt;
	&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;The code above is a pretty simple SELECT statement and a while loop through the results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Putting our information in the correct format&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we have our query sorted and looping through the results, we need to start creating the contents of our file. At the very top of the CSV file we might want to include column headings, so before our loop through the query results let's create the first line of our file.&lt;/p&gt;

&lt;p class="code"&gt;$file = "Product ID,Product Name,Description,Image,Price,Stock&amp;#92;r&amp;#92;n";&lt;/p&gt;

&lt;p&gt;Next, in our loop we'll start adding rows to the file, including each product's information.&lt;/p&gt;

&lt;p class="code"&gt;
while ( $record = $result-&gt;fetch_assoc() ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$file .= $record['id'].','.$record['name'].','.$record['description'].','&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;.$record['image'].','.$record['price'].','.$record['stock']."&amp;#92;r&amp;#92;n";&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;You might be wondering what the &lt;strong&gt;"&amp;#92;r&amp;#92;n"&lt;/strong&gt; is, this is formatting that will create a new line in the file. You can only use these in double quotes in PHP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Save the file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we should have a variable called $file that contains all the contents of our CSV file ready to be saved. We can do this like so:&lt;/p&gt;

&lt;p class="code"&gt;
header("Content-type: application/csv");&lt;br /&gt;
header("Content-Disposition: attachment; filename=".date('dmY')."_csvfile.csv");&lt;br /&gt;
echo $file;
&lt;/p&gt;

&lt;p&gt;This will cause the page to prompt the user to download our file when it is opened in a browser. You can call the filename whatever you like, in this example I have included a date for reference.&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/15/creating-a-text-or-csv-file-of-information-from-your-database/</link>
			<pubDate>Thu, 04 Dec 2008 9:50:06 +0000/</pubDate>
		</item>
		<item>
			<title>Using an autoload function to make your life easier</title>
			<description>&lt;p&gt;In PHP 5 you can make your life a lot easier by writing an &lt;strong&gt;autoload&lt;/strong&gt; function. This function will automatically load your classes, so you do not need to repeatedly include classes on every page.&lt;/p&gt;

&lt;p&gt;It is best to create an &lt;strong&gt;autoload&lt;/strong&gt; function in a file that is included on every page. Typically when I develop a site I like to have a configuration file that I include on every page which does database connections and other site wide operations, so I'll put mine in there.&lt;/p&gt;

&lt;p class="code"&gt;
function __autoload($class) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;require_once 'classes/'.$class . '.php';&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;What the above function will do is run whenever you create a new object and attempt to include the class file for you, on the fly! So now whenever I need to create a new object I just need to do the following, and the class file will be included for me.&lt;/p&gt;

&lt;p class="code"&gt;
$news = new News();
&lt;/p&gt;

&lt;p&gt;You can expand this function to be a little more intelligent. Firstly by checking that the class file exists, and secondly you may have more than one directory holding class files. This can be done like so:&lt;/p&gt;

&lt;p class="code"&gt;
function __autoload($class) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$classpath = 'classes/'.$class . '.php';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( file_exists($classpath) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;require_once $classpath;&lt;br /&gt;
}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$classpath = 'libs/'.$class . '.php';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if ( file_exists($classpath) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;require_once $classpath;&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;I hope you've found this article useful, if you have any comments don't hesitate to leave one!&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/14/using-an-autoload-function-to-make-your-life-easier/</link>
			<pubDate>Wed, 01 Oct 2008 14:16:02 +0000/</pubDate>
		</item>
		<item>
			<title>Choosing a PHP Web Host</title>
			<description>&lt;p&gt;When looking for a host for your PHP website there are a few things you should consider before you buy. Below I go over a few things to look for when choosing PHP hosting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP 4 or 5?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hosts are gradually &lt;a href="http://total-php.com/article/12/why-you-will-love-php5/"&gt;switching to PHP 5&lt;/a&gt;, and now that PHP 4 development and updates have come to an end, now is the best time to make the switch yourself. If you need time to make the switch some hosts will allow you to choose between the two, although be careful as sometimes this means &lt;a href="http://en.wikipedia.org/wiki/PHP" class="blank"&gt;PHP 5&lt;/a&gt; is running in CGI mode, and so is likely to take a performance hit. I would also suggest switching to MySql 5 too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linux/Apache&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You want to be looking at &lt;a href="http://www.webhostingsearch.com/linux-web-hosting.php" class="blank"&gt;Linux hosting&lt;/a&gt; with Apache rather than Windows hosting. Windows hosting is usually used for ASP/.NET applications, and if PHP is installed it is likely to be buggy and have poor performance. PHP is meant to be run in a Linux environment so let's keep it that way!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access to outside the document root&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With my PHP applications I like to keep template files and other system type files outside of the document root. This is a security thing mainly, as while your files are outside the document root a visitor in a browser cannot directly access them. Some hosts don't allow you to upload outside the document root however, and in some cases it might cause errors. I would suggest looking through a host's knowledge base, or even better contacting them directly to make sure you can do this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scripting requirements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You might be using a downloaded PHP script, such as Wordpress etc, or you might be using something you've written yourself. Before you buy web hosting it is best to check that any scripting requirements are available. This could be GD Library for image handling, or having register globals on/off, or having PHP in safe mode or not. A downloaded piece of software will usually tell you what's required, and if it's not on their website contact a host directly and they should be able to help out and answer any questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;General advice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above advice is geared towards PHP hosting specifically, it goes without saying that in general you want to look for good uptime, speeds, web space, bandwidth, easy to use control panel, good customer service, customer feedback and of course value for money. It's worth checking out some forums or &lt;a href="http://www.webhostingsearch.com/advanced-search.php" class="blank"&gt;hosting comparison sites&lt;/a&gt; for reviews and user ratings too.&lt;/p&gt;

&lt;p&gt;Hopefully this article will help you out when choosing the right hosting for your PHP site, if you can think of anything else to look for please leave a comment :)&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/13/choosing-a-php-web-host/</link>
			<pubDate>Mon, 22 Sep 2008 9:07:36 +0000/</pubDate>
		</item>
		<item>
			<title>Why you will love PHP5</title>
			<description>&lt;p&gt;If you haven't made the jump from PHP4 to PHP5 yet, here are a few reasons why you should, and why you will love it when you do!&lt;/p&gt;

&lt;p&gt;Recently I have seen increasingly more evidence that PHP4 is on its way out. Not only has support for PHP4 been stopped by the creators, but more and more &lt;a href="http://www.webhostingsearch.com/php-web-hosting.php" class="blank"&gt;php hosting companies&lt;/a&gt; are phasing it out too. Recently an old host of mine made the switch causing a few of my old sites to go down until I made the relevant fixes! This evidence has finally made me go for the change, and I have no idea why I didn't sooner...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New MySQLi extension&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The new mysqli extension makes database work a lot easier. In PHP4 I used a simple database class for all my database work, now there's no real difference between the two. Here's an example of the new syntax and how much easier it makes event the simplest of queries - getting a row from a table.&lt;/p&gt;

&lt;p class="code"&gt;
$db = new mysqli('localhost', 'username', 'password', 'database');&lt;br /&gt;
$result = $mysqli-&gt;query("SELECT * FROM table WHERE id = '1'");&lt;br /&gt;
$row = $result-&gt;fetch_assoc();
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;$row&lt;/strong&gt; is now an array of that row's information in the table. Obviously the above is a very basic example, &lt;a rel="nofollow" href="http://php.net/mysqli" class="blank"&gt;to read up on more mysqli functions check out the PHP docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PHP5 includes new XML handling functions, known as SimpleXML which, as the name suggests, makes working with XML a doddle! If you have an XML file to parse now all that's needed is a call to one function and you can access any of the information very easily.&lt;/p&gt;

&lt;p class="code"&gt;
&lt;em&gt;[myfile.xml]&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;
&amp;lt;item&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;description&amp;gt;Hello!&amp;lt;/description&amp;gt;&lt;br /&gt;
&amp;lt;/item&amp;gt;&lt;br /&gt;
&lt;/p&gt;

&lt;p class="code"&gt;
$somexml = simplexml_load_file('myfile.xml');
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;$somexml&lt;/strong&gt; is now an object that holds all the information that was in the xml file, which I can access like the following.&lt;/p&gt;

&lt;p class="code"&gt;
echo $somexml-&gt;item-&gt;description;
&lt;/p&gt;

&lt;p&gt;The above would echo out &lt;strong&gt;Hello!&lt;/strong&gt;. You can also loop through results nice and easily too. &lt;a rel="nofollow" href="http://php.net/simplexml" class="blank"&gt;Read up on more simpleXML functions in the PHP docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New Object Oriented features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many changes have been made to PHP's OOP. Anyone who has a background in using OOP languages such as Java, will know how poor PHP's OOP has been in the past. PHP5 introduces more strict rules for defining classes and methods, including the introduction finally of &lt;strong&gt;public&lt;/strong&gt;, &lt;strong&gt;private&lt;/strong&gt; and &lt;strong&gt;restricted&lt;/strong&gt;&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/12/why-you-will-love-php5/</link>
			<pubDate>Fri, 05 Sep 2008 13:41:10 +0000/</pubDate>
		</item>
		<item>
			<title>Browser based template editor</title>
			<description>&lt;p&gt;A common feature to CMS scripts is a browser based means of editing your templates. This can be helpful if you manage your site remotely or on the move quite often. In this tutorial we walk through how to create a simple template editor in PHP.&lt;/p&gt;

&lt;p&gt;This tutorial presumes that your system's templates are separated from your main PHP. You might be using a template language where all your templates are &lt;strong&gt;.tpl&lt;/strong&gt; files for example. The directory these templates are kept must be writable (CHMOD 777).&lt;/p&gt;

&lt;p&gt;This tutorial is split into two main parts: The form and the processing that occurs when you hit submit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The form&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First off we will need to open the file and get the current contents of the file, we will then populate a &lt;strong&gt;textarea&lt;/strong&gt; with these contents so we can edit them.&lt;/p&gt;

&lt;p class="code"&gt;
$file = 'template.tpl';&lt;br /&gt;
$handle = fopen($file, 'r') or die('Unable to open the file');&lt;br /&gt;
$templateInfo = fread($handle, filesize($file));&lt;br /&gt;
fclose($handle);
&lt;/p&gt;

&lt;p&gt;In the code above we declare a variable called &lt;strong&gt;$file&lt;/strong&gt; with the path to the file we want to open. We then use the &lt;strong&gt;fopen&lt;/strong&gt; function to open the file. the second parameter of &lt;strong&gt;'r'&lt;/strong&gt; is passed telling PHP that we only want to 'read' the file.&lt;/p&gt;

&lt;p&gt;Next we use the &lt;strong&gt;fread&lt;/strong&gt; function to get the contents of the file and put them in a variable that we've called &lt;strong&gt;$templateInfo&lt;/strong&gt;. The second parameter passed tells &lt;strong&gt;fread&lt;/strong&gt; how much of the file we want to get, in this case we pass the filesize of our template file so that it gets the whole file. Finally, we're done with the template now so we close the file.&lt;/p&gt;

&lt;p&gt;Now we have the contents of our template we can create a form and put the contents of the template in a &lt;strong&gt;textarea&lt;/strong&gt; for editing.&lt;/p&gt;

&lt;p class="code"&gt;
&amp;lt;form action="editor.php" method="post"&amp;gt;&lt;br /&gt;
&amp;lt;textarea name="template"&amp;gt;&amp;lt;?=$templateInfo;?&amp;gt;&amp;lt;/textarea&amp;gt;&lt;br /&gt;
&amp;lt;input type="submit" name="from_submit" value="Save" /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;
&lt;/p&gt;

&lt;p&gt;We now have a very basic form with a &lt;strong&gt;textarea&lt;/strong&gt; populated by our templates contents and &lt;strong&gt;'Save'&lt;/strong&gt; button. Now all we need to do is write the code that will process the form submission.&lt;/p&gt;

&lt;strong&gt;Form processing&lt;/strong&gt;&lt;/p&gt;

&lt;p class="code"&gt;
if ( $_POST['form_submit'] ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$file = 'template.tpl';&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;$handle = fopen($file, 'w') or die('Unable to open the file');&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;fwrite($handle, $_POST['template']);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;fclose($handle);&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;Other than checking for a form submission this code is very similar to before with a couple of differences. You'll notice that we open the file again, but this time using &lt;strong&gt;'w'&lt;/strong&gt; as the second parameter rather than &lt;strong&gt;'r'&lt;/strong&gt;. This tells PHP that we want to write to the file with completely new contents, so it opens it as writable and gets rid of everything that's in there, leaving an empty file for us to put our newly edited contents into. Next we use the &lt;strong&gt;fwrite&lt;/strong&gt; function to put everything that was entered into the textarea in the file. Close the file and we're done - template edited!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting more advanced&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You may want to get a little more advanced with this tutorial and have a fully featured code editor to help you edit your template. I would recommend checking &lt;a class="blank" href="http://markitup.jaysalvat.com/home/"&gt;markItUp!&lt;/a&gt; out for this.&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/11/browser-based-template-editor/</link>
			<pubDate>Thu, 04 Sep 2008 14:28:23 +0000/</pubDate>
		</item>
		<item>
			<title>Deleting files with PHP</title>
			<description>&lt;p&gt;Following our tutorials on &lt;a href="article/9/uploading-files-with-php/"&gt;uploading files&lt;/a&gt; and &lt;a href="article/7/listing-files-stored-within-a-directory-using-php/"&gt;listing files&lt;/a&gt;, this tutorial will walk you through &lt;strong&gt;deleting files&lt;/strong&gt; from a directory. This is done using the unlink() function. &lt;/p&gt;

&lt;p&gt;To start off, we create a function and pull through the file we're going to delete.&lt;/p&gt;

&lt;p class="code"&gt;
function file_delete($file) {&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;Then, we need to make sure that the file we're trying to delete isn't currently open. Confusing, we also have to make sure it's open before we do this!&lt;/p&gt;

&lt;p class="code"&gt;
function file_delete($file) {&lt;br /&gt;
&lt;br /&gt;
$ofile = fopen($file, 'w') or die("error opening file");&lt;br /&gt;
fclose($ofile)&lt;br /&gt;
&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;The last part of this function is to delete the file. Easy no?&lt;/p&gt;

&lt;p class="code"&gt;
function file_delete($file) {&lt;br /&gt;
&lt;br /&gt;
$ofile = fopen($file, 'w') or die("error opening file");&lt;br /&gt;
fclose($ofile)&lt;br /&gt;
&lt;br /&gt;
unlink($file)&lt;br /&gt;
}
&lt;/p&gt;

&lt;p&gt;Finally, we call the function from the page, passing through the file path for the file we want to delete.&lt;/p&gt;

&lt;p class="code"&gt;
file_delete("images/intro.jpg");
&lt;/p&gt;</description>
			<link>http://www.total-php.com/article/10/deleting-files-with-php/</link>
			<pubDate>Wed, 06 Aug 2008 9:49:57 +0000/</pubDate>
		</item>
	</channel>
</rss>
