Lezione 9: Supplement

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.

Introducing PHP: How it's possible

What is PHP and how does it fit into the client/server model?

Not just fetching a file

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.

Pandora's box: web pages written with programming

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).

Opacity of HTTP requests

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.

Architectural differences from JavaScript

But you've already got JavaScript, right? That is programming. How does server-side programming, such as with PHP, let you do anything different? Well, this is a good question, and not too uncommonly, the same kind of task could be done with JavaScript as with PHP. For example, you could also have JavaScript fetch the current time (although it would use the time on the client's computer rather than the time of a server). However, the division of labor usually goes like this.. JavaScript can rearrange and duplicate and beautify the content of the page, and it can also add new content that derives from the page's existing content or can be created from scratch (like the current time). With server-side programming, you can incorporate new information and new content into the page.

PHP language features

PHP is a fairly simple language and is arguably simpler than JavaScript when comparing their advanced capacities. The only large part of it is the large list of its functions, which you may memorize or look up pretty quickly (see the most common: string functions and array functions). It may be most effective to show the syntactic and language-feature differences between PHP and JavaScript. You can read these and then read the PDF for more depth on them.

Getting started with PHP

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.

The format of a PHP file

A PHP file is just text. A most simple one looks like this:

<?php print("Hello, world."); $x = 1; $x += 8; print("This is nine: ".$x); ?>

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.

Incorporating HTML

As we mentioned earlier, you can print HTML from PHP. You can simply do this:

<?php print("<strong>Hello</strong>, world."); ?>

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:

<?php print("<html>"); print("<head><title>ASDF</title></head>"); print("<body>Hello, world</body></html>"); ?>

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:

<?php print("<html>\n"); print("<head>\n<title>ASDF</title>\n</head>\n"); print("<body>Hello, world</body>\n</html>"); ?>

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:

<html> <head> <title>ASDF</title> </head> <body>Hello, world</body> </html>

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:

<html> <head> <title>ASDF</title> </head> <body> <?php print("Hello, world"); ?> <p>Another paragrpah maybe here</p> <?php print("This is nine: ".(3 + 3)); ?> </body> </html>

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.

Getting PHP to run on a server

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.

Things you're not gonna care about

You can skip these parts of the lesson:

Command-line PHP execution
Only for people who are interested in Unix programming
Resource types
Probably don't need to know about; PDF mostly gives lower-level details
Classes and OOP
A more intermediate/advanced programming paradigm, which the PDF expects you to know already, such as from another course; you can skip it
Hash functions
More for people in math & computer science; still probably understandable; can be used as another problem-solving tool in your collection
Transmission control and output buffering
More intermediate/advanced; doesn't commonly come up
Configuration options
More intermediate/advanced; doesn't commonly come up
Performance notes
For computer scientists; you don't need to know about performance just to get something going