Lianko

Enabling PHP Offline

Just as the title above says, I’m sure that you’ve been through this frustration… or well, you should have been, if the PHP includes tutorial you used looked something like this. So easy, yet when you try to view things offline, they most probably look anything BUT the way they should. So WHAT is wrong?

What is PHP and What Can It Do?

The most straightforward answer can, of course, be found on PHP[dot]net your first stop for all your PHP related issues. However, seeing that I know how comfortable you are at my site, I’ll quote said definition:

“PHP (recursive acronym for “PHP: Hypertext Preprocessor”) is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.”

from “What is PHP?

In other words, PHP is a free scripting language used for web development. I know you’ll be nodding bored, at this point, as I’ve yet to tell you anything specific. The answer to “What It Can Do?” is also found on the same site:

  • Server-side scripting. (e.g. the oh-so-popular PHP includes)
  • Command line scripting. (ideal for Linux based scripts or Task Scheduler – on Windows)
  • Writing desktop applications. (not the best choice, but possible nonetheless)

from “What can PHP do?

Now that we’ve established what PHP is and what it can do, it will – of course – come as a logical conclusion, that PHP needs a software (actually two) that will interpret its instructions and display the results after having executed them. Contrary to popular belief said application (or program – if you prefer) is not a web browser, but a PHP parser and, respectively, a

webserver (the latter is only needed for server-side scripting). These programs do not automatically come with your OS, so they must be manually installed.

Server-Side Scripting?? PHP Parser?? Webserver??

While you can very well use PHP’s include( ) function without understanding any of the three terms above, comprehending them might prevent you from spouting aberations like the one below:

“PHP is something that will make layout changes dead-easy. You will not have to edit every page when changing a layout. You will only need to edit the actual content. [insert php includes tutorial here]“

While the quote above is not 100% accurate, I HAVE actually encountered a webmistress that answered the question “What is PHP and what can it do?” with a similar answer. Or well… similar in meaning, at any rate. Now, let’s clear the three terms.

  1. Server-side Scripting: The http protocol (that you use to navigate on the internet) is based on a client-server interraction. The client, represented by the web browser, issues a request, that is then sent to the webserver to process and ultimately send the answer back to the client.

    That cleared, a server-side scripting language is a programming language used by the webserver. In contrast, a client-side scripting language is used by the client (web browser).

    E.g. You wish to connect to http://www.yahoo.com.

    So what do you do?

    You type said URI into your browser’s address bar, press ENTER and expect to see Yahoo’s home page. What you don’t see, is that after having pressed the ENTER button, this request is sent to the webserver, that tries to find said address.

    If the search is successful, it sends an answer back to the browser, and Yahoo’s home page will load in your browser window.

    If the webserver wasn’t able to find the address, it still sends back an answer, usually in the form of the oh-so-popular 404 error page.

  2. PHP parser – is the application responsible for interpreting and executing the PHP instructions. It will, first of all, evaluate the instructions (checking if they’re lexically correct; similar to a spellchecker). If the evaluation is successful, it proceeds to interpret and complete the actions requested; if not, you get an error message (e.g. parsing error on line 235).
  3. Webserver – is, in this case, an application (a software). You do not need to purchase one of those big boxes that you may find in the corner of your computer labs. This software has the role of reading through your website’s original code and send the result to the browser. If it encounters regular HTML code, then it will send it to the browser without further analysing it. If, however, it encounters PHP instructions, then the webserver will send them to the PHP parser that will interpret and execute them. The PHP parser will, then, send the result to the webserver. The latter will transform said result into (X)HTML code and send it on to the browser.

This is, of course, a very simplistic explanation of the three terms, but I hope to have at least shed some light over this thing called PHP and other entities it may involve.

Viewing Websites

As most of you know, one of the most popular, and easier to use, coding languages for creating websites is HTML (and – more recently – XHTML). While these two languages can satisfy your most basic needs (coding-wise), they don’t always manage to cope with all of them. This is where PHP comes in, ready to spice up your page, with on-line counters, personalized e-mail forms, commenting scripts and so much more.

Logically, since you will be embedding PHP scripts into your HTML coded web page, you will need a web browser to view the results. However, as explained before, in order to view things correctly you will also need to be able to correctly interpret your PHP instructions. This is what the webserver does: using the PHP parser, it reads your php code and then displays the result using the appropriate (X)HTML code. This way you will be able to reconstruct some of your online settings, necessary to successfully view your site.

Solution

You need to set up a virtual client-server connection. You can either do this manually, although if you’re mostly unaware of how to configure relative and absolute paths, this will all but kill you.

But don’t despair: nowadays there are a lot of ready-made webserver-PHP-Mysql installation packages that you can use.

A few of the most popular choices: