Page 1 of 3
PHP is a language that creates web pages and here we look at how to get the best from it.
It is assumed that you already know how to enter and run a PHP application. If not then consult Beginning PHP with Eclipse which explains how to download, install and use everything you need all the way to the first Hello World program.
What PHP does
You can think of PHP as a general purpose computing language if you want to, but it was designed with one task in mind and it is almost exclusively used for that task - generating web pages. So while it might be more flattering to PHP to introduce it in the widest possible context this would be misleading and it would make the job of learning how to use it harder than it needs to be.
So let's say the obvious to make it 100% clear.
- PHP is a language that creates web pages.
Again to state the obvious:
- The output of a typical PHP program is HTML
This means that to make any sense of PHP you also have to know about the web technology that the program is generating, and in particular HTML. In practice this shouldn't be a huge problem because HTML isn't difficult and mostly the way that PHP makes use of it is fairly simple. However, it is important to know that it is possible that you could have a problem with understanding a PHP program simply because you can't understand the HTML it is generating.
PHP as web server - CGI
The first thing to get straight is how a PHP program gets the HTML it generates to the client web browser that requested it.
This is surprisingly simple.
In the usual course of things a user asks for a web page, MyPage.html say, and this request is sent to the web server which finds the file MyPage.html stored locally on disk and sends it to the browser. So all a web server is doing is finding and sending pages stored on disk to the client web browser - this isn't a very sophisticated or intelligent piece of behaviour.
However, this isn't the only way that a web page can be served to a client. If the web server is asked for a web page that ends in something that it recognises as an executable program, MyPage.exe for example, then instead of sending he file MyPage.exe to the client in the hope that it might contain HTML after all the web server loads and runs the program on the server and collects whatever its output might be and sends that to the web browser.
Of course this allows the web programmer to create a program that generates HTML and have this executed and its output sent to the browser without the user really being aware of any difference - except perhaps the use of a "funny" file name.
This is the CGI or Common Gateway Interface and it has been in use for a long time as the basic way of generating web pages. Today there are some improved versions of the CGI designed to make the process more efficient, more powerful or, in most cases, just to hide the fact that this is what is going on from the programmer. Web development systems such as .ASP, PHP and so on essentially are still using the CGI idea even if the mechanism has been tinkered with.
To return to PHP this is exactly how it all works.
You write a PHP program, MyProg.php, and when the client asks for this as if it was a web page then the server loads and runs the program MyProg.php and sends any output it creates as if it was HTML to the web browser client.
Notice that the server doesn't check the vailidity of the program's output in anyway. The program can produce complete rubbish and the server will still pass on its output.
Also notice that the PHP program isn't running on the client's machine but on the server and all it can do is send the client some data that will be rendered as HTML. That is, PHP is a server side language - it runs on the server and its interaction with the client is limited to sending it a web page.
You might think that server side languages are limited, and in a sense they are when it comes to user interaction, but there are ways around this problem. First we have to master the basics.
There are two distinct ways that PHP allows you to generate output to be sent to the client and these two are confusing and cause a lot of problems for programmers.
A program consists of a list of instructions that are obeyed one after another. You tell the machine to do something, it does it and then you tell it to do something else. So for example, a very simple PHP program, which is a slight modification of the "Hello World" program in Beginning PHP with Eclipse, is:
echo 'PHP' ;
The instructions that make up the program are three "echo" commands which simply take whatever you have entered between quotes and send it as output to the browser. Notice that a PHP program always starts with <?php and ends with ?> and that these two tags bracket the list of instructions. Also notice that every PHP instruction has to end with a semicolon - if you forget the semicolon you will see an error message.
If you run this program, **remembering to start the Apache web server, then the web browser will show:
Notice that running the program is just a matter of providing the appropriate URL to a browser - http://localhost/Learn%20PHP/Hello.php, for example. The simple act of the browser asking the sever to load the web page called Hello.php invokes the CGI mechanism and runs the program returning to the browser any output it generates as the web page.
At this point you might question the idea that the PHP program has output HTML - but plain text without any markup tags is valid HTML and any browser will show the text unformatted using a default font etc.
You might also notice that there are no spaces between the words. This is just a result of the fact that what you send to the client browser is exactly what you typed - where are the spaces between the words? To make it look better change the program to have a space at the end of each word.