Learning PHP – Part 1: Getting Started
An Egyptian Bazaar (from The Commons)
Having finished off my HTML and CSS review and learning, I had a hard time deciding where to go next. Having already learned some JavaScript previously, I poked around in the W3schools tutorials for it, but ended up deciding to tackle PHP and MySQL.
Part of the reason I decided on PHP and MySQL is because I've had a book on the two technologies sitting around for some time. So I've been working my way through PHP and MySQL for Dynamic Web Sites (2nd Edition) by Larry Ullman. So far, in my humble opinion, it's an excellent book. I'm finding it is getting through the material at a good pace. The progression is quite logical from one chapter to the next, and the examples are quite relevant and useful. Since the book has been sitting around for awhile, it covers PHP 5 (which is now at version 5.3.6 a.k.a. PHP 6) and MySQL 4.1 (now at version 5.5). Obviously this makes it a bit out of date, but as far as I understand, the versions that I'm looking at are both still widely used, and the newer versions aren't a whole lot different.
Before jumping into the actual work I've done in learning PHP and MySQL, I figure I should give a bit of an introduction to what exactly PHP and MySQL are and what you do with them. The book also does this, which wasn't exactly that useful for me since I've got a fairly good undstanding of many web technologies, even if I don't know the details of how to actually use them.
What is PHP?
PHP originally stood for "Personal Home Page" and was made by a fellow named Rasmus Lerdorf in 1994 to track visitors to his online resume. It's since gone through various revisions and has grown to become one of the top server-side scripting technologies, and now stands for "PHP: Hypertext Preprocessor". The fact that it's both free and easy to learn has made it a prime competitor for other server-side scripting tech like Microsoft's ASP. If you'd like to learn more, take a look at the article on PHP from Wikipedia.
A "server-side scripting language" is one whose code is run on the server before serving up a page to the client. This is quite the opposite of client-side scripting languages, JavaScript being the most obvious example. JavaScript code, while similar, is served to the client as part of the page, and the client itself will run the script(s) on a page as needed. For PHP to work however, you must have the PHP engine installed on the web server, as PHP is not meant to be sent to the client like JavaScript.
Here's a run through of what happens when loading a PHP page:
Client requests page from server
Server finds file, sees it has PHP to be run, passes to PHP engine
PHP engine runs code, creates page to be delivered to user
Server sends completed page to user to view
JavaScript on the other hand, works rather differently:
Client requests page from server
Server sends page to client
Client reads JavaScript, executing it on load or when the page is interacted with (depending on the nature of the script)
The above is obviously a simplification, but demostrates the general idea. While there is some overlap between what you can do with PHP and JavaScript respectively, the client/server distinction makes the two useful for rather different tasks. Beyond this, the two work extremely well together. For instance, I've been learning about setting up form validation (making sure that all fields are filled out and contain correct data in a form on a web page). While this can be done with PHP, because of it's server-side nature, it requires submitting the form to the server, and reloading the page if the validation fails. From a user's perspective, this can be slow and inconvenient. JavaScript is more than capable of doing form validation on the client's side, allowing it to happen immediately and not have to needlessly use up bandwidth and the server's resources.
PHP allows you to set up dynamic websites, where how a page is displayed can be altered based on user input among other factors. It allows basically static parts of the site (like the header and footer) be set in one place and called by every page on the site, greatly simplifying making changes that would otherwise have to be repeated for each page. It also has the ability to interface with a database (this is where MySQL comes in), allowing even greater automation, particularly of content management. Many popular Content Management Systems (CMS) like WordPress, Joomla and Drupal are built using PHP.
What is MySQL?
MySQL is an open-source database system. It's available at little to no cost, and offers features on par with much more expensive database software. SQL stands for Structured Query Language, and was invented in the 1970s for the Relational Data Base Management Systems (RDBMS) as laid out by Edgar F. Codd's relational model. These RDBMS use multiple tables to store various sets of data and use various methods to build a web of interrelations between the tables. The system is much more effective than a simple flat file database (basically, a spreadsheet) and offers a range of features unavailable in simpler DBs.
MySQL is one particular SQL based database system. Since I'm going to be covering how to work with MySQL and will have an intro post for it as well, I'll leave this part quite short. In relation to PHP though, it's important to note that PHP has a whole variety of functions that deal with storing and retrieving data from MySQL databases specifically. Going back to the example of the process by which a PHP page is retreived; if the PHP on the page is required to insert or read data from a MySQL database it does so at the same time as the server runs the PHP code.
I think that about covers what I wanted to touch on with this introduction. I'm looking forward to actually getting into the stuff I've been learning. I'm already almost half way through the book, so lots to cover. I've been spending more time working on the stuff than I have been updating the blog. I'm sure I'll push through and end up with a backlog of posts queued to go up though. In other news, I haven't been following through with my plan to get seriously back into Starcraft 2. Too many other things going on lately, but I did play a bunch of Shogun 2 again on the weekend.
No related posts.

June 6th, 2011 - 22:42
Thanks for the nice words on the book and good luck with your learning!
June 7th, 2011 - 12:51
Thanks Larry! I’m having a great time and can’t get through the material fast enough it seems.
June 7th, 2011 - 13:05
… sooo cool that Larry Ullman replied!