Introduction to PHP

This is the start of a series of tutorials for beginners on how to write your own PHP. I am going to break with the usual programming tradition though and not show you how to get PHP to display the words "Hello World" in your web page since there is no point in using PHP to do that since HTML can do that quite well without PHP. Instead we are going to start by looking at what you need to do to set up a web page that can use PHP instead of being purely HTML. PHP and HTML can coexist in the same file and so there is little point in having PHP output HTML where just using plain HTML will do the job just as effectively and be easier to read.

One thing that we will be looking at in later tutorials is breaking up your server side processing into various layers the way you should be doing with your client side processing so where on the client you have a content layer, an appearance layer and a behaviour layer, you can also break your server side processing up into layers as well for such things as template, business logic, data access etc. Where the server side breakup differs from the client side is that each server side layer does not necessarily consist purely of one language. While the template layer will be mostly HTML it will also include some PHP and while the data layer will be mostly SQL it too will contain some PHP. For the simple examples we'll be looking at in the first few tutorials our code will not be complicated enough to start breaking it up into layers like that though so we'll leave looking at that aspect until we get past the basics. I am bringing it up here though so that you can keep in mind that we will eventually want to set up our code that way and will not fall into the trap of learning just the basics and then writing a jumbled mess for all your pages.

Let's get started.

The easiest way to change a web page to allow PHP code to be added to it is to change the file extension. While the file extensionis not used directly by the server in determining what sort of file it is and what to do with it, there is a table built into the web server that maps file extensions to MIME types so that the server can determine what MIME type a file has from its extension. To have the server recognise that your file contains PHP that you want it to run before serving the page to the browser you can simply change the file extension for the file to .php.

The alternative to changing the file extension is to change the table in the web server to map a .html and .htm file extension to the PHP MIME type. How you do this will depend on which web server you are using. It also has the effect of adding an overhead for all of your web pages that don't contain PHP because they will all need to be passed through the PHP processor in order to see if there is any PHP to be processed before passing the page to the browser. This method is therefore not advisable unless all or nearly all of yourexisting web pages are going to be updated to contain PHP.

Having changed the file extension so that the server will actually look for PHP in the web page we next need to know how to add PHP to our HTML. There are actually four ways to add PHP into your HTML, two of which always work and two of which will work provided that they are turned on in the web server.

The two ways that can be turned on or off are the two shortest ways of defining PHP. These are <? ?> and <% %>. Where the appropriate tag option is turned on to allow one or both of these anything within the tag will be treated as PHP. Since the first of these variants can potentially get confused with XML tags your coding is made much simpler where that option is turned off and you don't use it. The second of these variants is even less commonly used as that tag is mostly used with other languages that are alternatives to PHP. I recommend that you don't use either of these tags even if your server does support them if only because they are less portable for if you ever need to move your site somewhere that doesn't have them enabled. I have mentioned them simply so that you know that they exist and so you can recognise them if you see them used in someone else's PHP code examples.

The two ways that are always enabled for defining PHP are <'?php ?> and <script language="php"> </script>. Of these you will almost never see the second one used basically because both of these do exactly the same thing in exactly the same way and the first is so much shorter than the second.

You can insert as many of these tags into your HTML as you need in order to be able to incorporate PHP into your HTML and while there are PHP commands that can be used to generate HTML your code will be much easier to read and maintain (as well as faster to run) if you actually use plain HTML in your page as much as possible just switching into PHP to generate those parts of the page that involve dynamic content. I'll show you examples of exactly what I mean by this in later tutorials.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow