Archive for the ‘Learn PHP’ Category

For those who want to understand the secure PHP contact form that I wrote and released last month, here is part 1 of the dissection of the code with the whys and hows.

I was planning to just do one post with a complete dissection but I've had this sitting here for a couple of weeks and am just a little pushed for time right now, so I'll get the parts out as and when I've got a chance! I'll link all of the posts together once they're all done.

Continue Reading

I've written up an easy to use, secure, accessible and XHTML Strict compliant contact form, originally for someone to use on their site, but figured I'd post it up here as a bit of a PHP tutorial for anyone who wants to understand what's going on, or for others to just download, follow the instructions and use. I'll explain its usage today and then dissect the form for any PHP learners later in the week.

First off, download the contact form, unzip it and open it in your favourite coding editor. The best version of this form requires PHP 4.4+ however I've altered the line that depends on this version so that you can use it down to version 4.0 however foreign characters will not be checked in the name. Both are contained in the download file.

Continue Reading

Anyone who's wanting to learn PHP and MySQL should be considering SitePoint's Build Your Own Database Driven Website Using PHP & MySQL by Kevin Yank. I've just finished reading this book and have to admit, I wish I had this book to learn from when I first learnt PHP a few years back.

You may be wondering why I've been reading a book that's clearly for beginners. Couple of reasons. I learnt PHP when version 4 had just been released. The book I learnt from was out of date within weeks simply because of the major changes that happened, default settings etc. Whilst it was a great book to learn from there were parts that I'd either skipped (Object Oriented Programming for a start!) or just parts I'd never really grasped. Knowing how well SitePoint books are written, I decided to buy this book to give me a chance to refresh my knowledge, pick up on any changes for PHP 5 and also have an up-to-date PHP book on my shelf.

So what will this book teach you? It will teach you the basics of PHP and using MySQL. It explains how to use PHP to create dynamic webpages, how to store the content of a site in a database and retrieve that information and display it on the site. How to allow people to add to and update that content. It goes into regular expressions, the basics of PHP's file access methods, good structure using includes and more.

I recently had a chat about this book with someone who wanted to learn the basics a bit better but had looked at the free chapters of this book and got completely confused by its complexity. If anyone else has been or finds themselves in the same situation then I know where you're at. Ignore the first 2 chapters initially! The first chapter goes into how to set up PHP and MySQL on your computer. Whilst I used to do this myself now I recommend using Xampp. It's easy to install, comes with everything you need and can also be run off an external drive or USB pen drive, so you don't even need to put it on your computer! Then the second chapter of the book goes into MySQL. Again, not something you need to concern yourself with at first. So skip to chapter 3 and things shouldn't be as complex and scary!

So if you want to learn PHP then I highly recommend this book for the basics. You can get 4 free chapters from the SitePoint page or you can buy the book cheaper with free delivery from Amazon UK.

SitePoint have released the 2nd Edition of their PHP Anthology: Tips, Tricks and Hacks. Not that I can ever remember seeing a 1st Edition of this book (else it would have been on my bookshelves!). It looks to be a pretty good book, I've just grabbed the free chapters to have a look through.

For anyone who's learnt the basics then this book is probably a good intermediate level book before stepping into the advanced stuff. The free chapters are available from their site and worth a look if nothing else. Alternatively it's available at Amazon UK as well (although for once the SitePoint price is cheaper with the current exchange rate!).

Alternatively, on this SitePoint post you have a link where you can get $10 off the book, plus a free (usually $10) PHP Reference Poster. So that would be £20 for the lot including delivery. Ack it's tempting…!

Update: The First Edition of this was the PHP Anthology Vol 1 and 2 – two books written by Harry Fuecks. SitePoint have now merged these two into one and brought on a load of new authors. I'm guessing it could be quite different in the way it's written compared to the first edition, however I'd have to compare the sample chapters to the books I have. However, if you've learnt PHP and want to progress then it's the best next step you could take :)

Arrays are such a useful part of PHP. It's a way to store multiple pieces of information in one variable, and they can be very powerful and timesaving when used in the right way.

There are a couple of ways to create an array. The first is writing the whole array out in one go eg.

Array Code
  1. $testarr = array("Bob", "Sally", "John", "Pete", "David", "Jane");

This is fine if you can create the array with one line, however as your PHP progresses you'll find you use an array to capture data from say a form submission. So an efficient way of doing this, and another way which you can create an array, looping or otherwise, is

Array Code
  1. $testarr = array();
  2. $testarr[] = "Bob";
  3. $testarr[] = "Sally";
  4. $testarr[] = "John";
  5. $testarr[] = "Pete";
  6. $testarr[] = "David";
  7. $testarr[] = "Jane";

Note the first line initialises the array. This is for security to ensure that nothing has been passed into the array eg. via the URL, that we don't want in there.

With this array you can use it in a variety of functions and uses. For a full list of the functions available read the Arrays page on PHP.net. Examples of usage are

  • Re-sort the list and display in alphabetical order.
  • Compare a variable to the array to see if the variable exists within the array.

To simply list the content of an array you can either do the quick and easy method using print_r()

Array Code
  1. print_r($testarr);

Or you could do it a little neater using the foreach loop.

Foreach Loop Code
  1. foreach ($testarr AS $key => $value) :
  2. echo "Item ".$key.": ".$value;
  3. endforeach;
Page 1 of 712345»...Last »