Sunday, October 22, 2006

PHP 5, The Promised Tutorial Part 1

Ok, from a previous post I got some requests around how people can get started with PHP. I am going to attempt posting a series of tutorials here to get you started.

Disclaimer:

I do not claim to be a teacher at all, and if you benefit from this, please let me know. Also, there are no substitute for purchasing a good solid book on the topic to get started, leave some comments at the end of this post if you would like some suggestions on good books to get started.

Ok, getting started.

A quick explanation of what PHP is in order. I will not attempt to give a fancy explanation of PHP here, that is what Wikipedia is there for. Basically PHP is a Hypertext Preprocessor. I will attempt to explain what this means.

If you enter an address such as www.somesite.com/index.php the following happens:

  1. Your browser does a DNS lookup on the domain part to see where exactly www.somesite.com is located, this DNS lookup returns and IP Address, IP standing for Internet Protocol.
  2. Once it finds out where www.somesite.com is located, it will connect to the server and REQUEST the file index.php From the webserver.
  3. The webserver will locate the index.php file on disk. Because the file has a .PHP extension, instead of just sending the content back to your browser, it will First call a file called php.exe, or php.cgi etc, and tell this file to Process or Parse Index.php.
  4. Php will execute all the lines of PHP code (php is included in tags inside the file) to produce a result, and this result is plain HTML, which is then returned to your browser. This return is called a RESPONSE.
Ok, hope I did not lose anyone yet, if you feel slightly lost, hang in there, it will become just a little more clear soon enough.

Perhaps I should explain this by an example. For the first part of this tutorial I will write a classic, Hello World. By the end of this tutorial you should understand the following:
  • Embedding PHP inside HTML files
  • Handling PHP variables from Forms (Include Both GET and POST)
  • Process forms submitted to PHP.
First things first. You need a webserver and PHP installed. Personally I recommend WAMP, this installs Apache, MySQL and PHP which is all you need to get started with PHP. It is easy enough to install, but leave a comment is you cannot get this done.

Once you have WAMP up and running, create a new project, easiest way is to create a new folder in your document root on Apache. If you stuck to the default WAMP install, it should be c:\wamp\www.

Call this new folder hello_world. In here you can create a new file called hello.php.

Open hello.php in you favorite text editor, I use Cream/VIM or Notepad, both work just fine, but you would want to eventually look into something with a tad more features such as proper project/folder management etc, I use Macromedia Dreamweaver for this, but you are on your own here, find an Editor that works for you.

Ok, let us start our file. Let's do the form first...

Type the following into your file (Click for a large view):



Once you have done this, save the file and enter http://localhost/hello_world/hello.php into your browser - If it is not Firefox, get it here - and you should see the following:

Ok, now that's done, if you click the submit button nothing will happen, but you should see your location changing to http://localhost/hello_world/hello.php?action=1.

Now we want this thing to start doing something. Type the following PHP code into the top of the hello.php file.

If you view the page now, and enter your name, you should see the following:




In a nutshell, that was it. Time for a quick explanation...

In the HTML, you created a FORM Element and named it main, you specified the FORM's METHOD as POST, and the ACTION as hello.php?action=1.

The two important bits here is the Method and Action. Method refers to how the form should be returned to the server. This will be POST 90% of the time, but it can also be GET. In very rare conditions you might use mail as well, but this is highly irrelevant for our exercise.

POST means your browser will take all the elements inside the form such as Text boxes, dropdowns, checkboxes etc, and compile this into and HTTP POST request, one day when you are really bored you can read up about this in more detail, and then sends the POST to the URL specified in the forms Action part.

In the action part of the form, I included a querystring variable called action=1. Basically, the querystring is one of your biggest friends in PHP and other web languages for maintaining application state, we will get to that later on. The querystring is a set of values appended at the end of a URL, and starts with a ? After the .php extension. Additional variables can be included using & as a separator, but don't worry about this for now.

Once the POST reaches its destination, you can access a number of variables as outlined below. I am assuming that you are familiar with what a variable is.

These variables are as follows:

$_POST = Array containing all elements posted to your PHP script.
$_GET = Array containing all the variables passed in the querystring.

There are others such as Session and Server variables, but I will explain these later on.

So, we have now told the form where to go, and it is time to explain how our PHP script does its magic.

At the top of the page we start with a block of PHP script. PHP scripts are always included inside PHP tags, which is for closing tags. When the server passes our hello.php file through the PHP executable, PHP will parse everything between these tags.

First we check if our form was submitted. We do this by checking if action was included in the querystring, and if its value was set to 1, this is done with the simple if statement at the top of the page:

if($_GET['action'] == 1)

Please note the use of == which is the comparison operator, which should not be confused with the assignment operator =

If we have the action GET variable and its value is 1, then we generate our hello message and we store this in a variable. Else, we do not generate any message.

Note in this line of code

$hello_message = "Hello there " . $_POST['yourName'];

We use the assignment operator (=) here to assign a value to the $hello_message variable, and we also use the concatenation operator (.) to concatenate the contents of the $_POST['yourName'] value to our message.

Remember the textbox on the FORM was called yourName? That is exactly what you see here, it was included as a POST variable by your browser automatically when submitting to the webserver, and here we access it to generate our message.

Last but not least, we have another set of PHP tags inside our HTML, this line simply prints the hello message we generated and stored in our $hello_message variable to the browser.

This is where we are done with my first, very brief and basic tutorial. I trust that this did give you a small understanding of the very basic concepts of PHP, and exactly how interesting the journey could become going forward.

I encourage you to play around with writing code and sending it to servers and back and seeing what happens. Our next tutorial will be a lot more interesting, when we write our simple calculator class and create a basic online calculator. This tutorial will show you exactly what a class is, and try to explain the basics of inheritance.

Please leave some comments to this, so I can get an idea of how I should change the format of these articles, I hope you found if valuable.

2 Comments:

At 1:33 PM, Blogger SteelJaw said...

I used Xammp. Is it also ok?

Cheers,

SteelJaw
http://steeljaw.blogspot.com

 
At 10:23 AM, Blogger l09f1l3 said...

Yeah, that will work just fine...

 

Post a Comment

<< Home