A supplement for non-computer scientists reading Website Architecture Lezione 9: PHP.
Most of the PHP lesson should be pretty understandable. There are just a few things that you can skip because you probably won't care about them until you are more experienced in programming. This supplement also gives a short introduction to PHP and how it is incorporated into websites.
What is PHP and how does it fit into the client/server model?
Normally, when a client makes an HTTP request, he is asking for a file, then the server goes and gets the file and sends it back within an HTTP response. In the case of PHP, the server is not simply fetching a file and sending it back. The server is running a program which produces text output (as do most programs), then sending the program output back to the client as if it were the file that the client wanted.
Pretty much any kind of programming environment - C++, Java, and others - can write text output and does so by deafult (i.e. making a Windows program is an exceptional and more difficult case). And, by extension, they can write text output which has HTML tags in them - just add some of those alligator brackets (<tag>) in the text output, and you're in business. And with that, you can write HTML from a program.
PHP is a languge that is simply more convenient for the typical demands of writing web pages. It has lots of functions for manipulating strings, working with tags, manipulating arrays, and reading & writing HTTP headers.
Once you've established that you can write a web page using a program, an entirely new world of possibilities is opened. You could make a website that looks different every time the user loads it, such as with a hit counter or an up-to-date stock price. You could save data about the user's visits and use the data to inform the output of the program's future runs, or you could let the users contribute and build the website (i.e. use a database; e.g. Wikipedia).
So we've established that HTTP requests may sometimes result in a file being fetched and may sometimes result in a program being run. Does the client know what happened on the server side? No. There is nothing in the HTTP protocol which exposes whether a program has been run or whether the response was simply a file. There may be some typical giveaways, but you as the server-side programmer could cover them up. Strictly speaking, the client cannot know for certain and shouldn't care.
Getting started with PHP is pretty simple. If you have your web server and FTP program already set up, and you know how to drag and drop files to the server, you can get started with PHP in 5 minutes.
A PHP file is just text. A most simple one looks like this:
The "php tags" at the beginning and the end are necessary. Actually, the opening one can be just <?, but this could possibly be ambiguous with other languages (XML), so it's nicer to put the whole thing.
As we mentioned earlier, you can print HTML from PHP. You can simply do this:
But is this really valid HTML? Well, there are many parts missing from the resulting HTML document; it only has this one <strong> tag. You still need the <html>, <head>, <body>, etc. So there are typically two ways people incorporate these tags in the output of a PHP program.
The first way is simply to cram all of those tags into the print() statement - something like this:
By the way, all of this print() output is going to produce one line of output - no line breaks in between. Similar to how you have to do <br> in HTML, you have to do an explicit line break when printing in PHP. You must do something like this:
Note that the "\n" does not translate to a <br> tag; if you want a <br> tag, you still have to write a <br> tag. This just puts a line break wtihin the HTML text, so it produces something like this:
Now - the second method of incorporating HTML into PHP. This is interesting. A PHP document could actually be seen as an HTML document which has segments of PHP inside of it. Like this:
Our first example, in which the PHP file started and ended with PHP tags, was actually a special case of this above example, in which the entire file was a PHP segment.
In practice, both are pretty common; having only PHP tags, no HTML incorporated into the document, is perhaps more common and is certainly more common in more complex sites. But in the meantime, you might as well use the HTML + PHP method, because it is less complicated.
Most/all web servers can already run PHP, so there's no real setup cost. But still, you need to know when PHP is executed and when it isn't - e.g. when an HTML file is simply sent back to the client.
By default, only .php files will be executed by the server. You could configure this, so that other files (.html, .whatever) are also treated as PHP and executed, but most people don't care to do this.
So, the bottom line is that you name it as a .php file, put the PHP tags in there, and it will work. If you upload myfile.php, you can open a browser and go to http://www.mysite.com/myfile.php and it'll work.
Note that if you put PHP tags inside of an .html file, it will not simply work.
You can skip these parts of the lesson: