PHP Part 1 — Getting To Know You
Hello and welcome to the first part in a series of articles dedicated to giving you all you need to know about using PHP and MySQL to create dynamic websites.
We’ll kick off with some history....
In The Beginning...
Back in the day (1994 to be precise) there was a man.
That man went by the name of Rasmus Lerdorf, and he was a man with a problem.
He wanted a dynamic webpage of his own, but none of the solutions he could find were any good to him.
Most interactive sites back then were made on huge budgets, using custom built components, so he had one choice: build a custom solution himself.
So that’s exactly what he did, creating a web programming language based on C.
And he called it Personal Home Page (PHP). And it was good.
So he released it to the world, for free, and let people pick holes in it, and they did, in their millions.
As the entire source code was released for free, and anyone could change it as they saw fit, it soon blossomed into the language we see today, undergoing three major rewrites to become PHP4.
It's acquired a new name (PHP now means “PHP: Hypertext Preprocessor”) to reflect it’s open-source design, many new functions and features, and continues to grow under the watchful eye of the good people at www.php.net.
Is It Any Good?
Of course it is, how could you even question it? Seriously though, it’s good for these reasons:
- It runs on almost anything — You can run it on just about any operating system out there, so no matter what you are using as your server (or even if you want to do work on your Windows PC at home) it’ll be quite happy.
- It’s free — You can get hold of it for nothing. Which has got to be good.
- It’s secure — Not only can users not see your PHP code, but with well over 5 million domains using it, and the source code available, bugs are found and fixed far quicker than closed source solutions.
- It’s fast — PHP runs hellofa quick, and since PHP4 now runs with something called the Zend Engine, it’s got even faster.
- It works great with databases — PHP has excellent support for MySQL databases, and makes it so simple to get data from them.
- The online support is amazing — You won’t find a better centre for resources on a language than www.php.net, plus there are thousands of other forums etc if you still need help.
How It Works
PHP is an embedded scripting language, so all you do is take your existing HTML pages, and stick bits of PHP into it where you see fit.
The only thing to note is that your web server where your files are sitting may only execute PHP in pages that have certain extensions, so PHP web pages generally have the extension .php instead of .htm or .html.
When a person browsing your site requests a page, the web server finds it, goes through it and executes any PHP it comes across, before sending the finished version to the browser.
The user can never see that PHP that went on behind the scenes, all they see is the finished article.
The web server can tell which bits are PHP, and which are plain old HTML, by looking at the tags that surround it. PHP will open with a <?php or <? tag, and end with a ?> tag.
Generally you would use the shorter <? tag, unless your server was set up to handle XML documents, as they use the same tag, so you’d have to use <?php.
Here’s a little example:
<p><?php echo '<b>Hello</b> World'; ?></p>
This would be sent by the web server to a website browser as:
<p><b>Hello</b> World</p>
We’ll cover the language itself in lesson two, but for now we’ll move on to MySQL...
MySQL and PHP Sitting in a Tree...
OK, so you’ve got a handy way of manipulating data with PHP, but unless all you want to do is tell people the time with commands like <?php echo date("d/m/Y H:i"); ?>, you’re going to need somewhere to get some data from.
MySQL (pronounced My Ess-Que-Ell or My Sequel) is a Relational Database Management System (RDBMS).
In layman’s terms, this means it’s a place where you can store loads of data, and it gives you a way to find the data you want.
MySQL is ideal for use within a PHP built website, as PHP knows just how to sweet-talk MySQL into giving you the data it wants.
MySQL is slightly different in it’s origins to PHP, as it is developed and supported by a private company called MySQL AB.
However like PHP it’s free — unless you want to sell it to someone as part of a product, in which case you have to pay MySQL AB.
All you should need to know about this is that you can use it for free on your website, because you’re not selling MySQL.
To give you some idea of what you would use MySQL for, on this site I use it to store visitor stats, topics, user details, messages, posts etc.
Other sites use it for taking orders, holding product details and many other uses.
Advantages of MySQL
The advantages of MySQL are quite like those of PHP:
- It’s fast — Even more so than PHP, it was designed with speed in mind. It’s reckoned to be about the quickest RDBMS out there.
- It’s free — Another reason why it’s so damn popular.
- It’s easy to use — It uses standard SQL commands.
- It runs on almost anything — Just like PHP.
- It’s secure — MySQL has a great permissions system to make sure that people don’t get at your data when they shouldn’t.
- You’ll never fill it — MySQL can theoretically handle tables up to 8 million terabytes in size — if you can find a web server that can store that much.
- It has great online support — And if that’s not good enough, MySQL AB provide support for businesses who need serious support.
Setting It All Up on Your Home PC
I hope by now you’re convinced of PHP and MySQL and their potential, so let’s get you set up and ready to start playing with them on your computer.
These instructions are for users of Windows 95 or later, but Mac and *NIX users should be able to find the appropriate packages online without too much hassle.
Step 1: Getting Your Personal Server Going
The first thing you will need is a nice easy program to install Apache Web Server, PHP and MySQL onto your system.
Luckily, you’re not the only one to have ever done this, all you need do is get yourself a copy of phpdev from http://www.firepages.com.au.
I would recommend getting a stable version rather than a beta one.
Installation is a breeze and should take minutes.
Words of warning: I have found problems if you attempt to install this to a drive other than C:\, so try and install it here if possible.
Also, you may run into problems if you already have a Web Server such as IIS running on your PC (usually used for ASP), I would advise you remove this before installing Apache Web Server.
Once up and running, you should find that by pointing your favourite browser at http://localhost, you will see a directory with some default files in it.
This is your personal web server’s root directory, and you can add files to it at C:\phpdev\www (where C is the drive you installed phpdev on).
Step 2: Getting a Decent Editor
You can, if you wish, now go about creating PHP files in a simple text editor like Notepad, however I would recommend you download PHP Coder Pro!, a simple PHP editor that will help you immensely.
It includes lots of little things to help you along, including auto-complete of some code, easier code indenting, line numbering and code colouring.
These little things will make it a lot easier for you to spot your mistakes as you go along, and save hours of aggravation.
So What Have We Learned?
Today you will have gained a better understanding of what PHP and MySQL are, and be set up and ready to get down to some serious coding.
In part 2 of the series, you will be introduced to the basic operators, variables and data types used in PHP, and begin to use these ideas to add dynamic content to web pages.