PHP basics

PHP stands for PHP: Hypertext Preprocessor. No, that doesn't make sense, so don't even try. What it means, though, is that PHP is a programming language where all the commands are executed by the server, rather than at the user's end by the browser (like Javascript).

This is useful for two reasons. First, it avoids the use of Javascript, which some users have turned off on their browsers for security reasons. Second, PHP script is completely invisible to the end user. Try viewing the source of any page online that ends in .php, you won't see anything besides normal HTML. It's an easy way to avoid cheating by someone viewing the source on the games and just jumping to the answer pages. Plus it's great for the owner, since you don't have to worry about encrypting all of your code.

is PHP on your server?

It's very simple to add PHP code to any HTML document. But first, PHP must be installed on your host's server. If you're not sure, try this simple test.

Open your text editor, type these three lines, and save the file as "phpinfo.php":


You can write all the code on one line, or on as many lines as you like as long as the block of text has <?php or <? at the beginning and ?> at the end. The above example would also work if it were written as <?php phpinfo(); ?>.

Three important points shown in this example!
- A file must always end in .php in order for the script to run.
- All PHP script must be contained within <?php ?>, or <? ?> (the shorthand version). This is similar to how all HTML code must be contained within < >.
- All commands end in a semicolon. It's very easy to forget to add or mistype a semicolon at the end of a line, so always check for that first if you run into problems.

Upload phpinfo.php and bring it up in your browser. If your host supports PHP, a long page full of information should come up with the words "PHP version" and the version number at the very top. If nothing happens, then contact your host and ask if they support PHP or if can install it or their server. Otherwise, you'll just have to find another host.

Remember the version number!
New PHP scripts might not work with older versions of PHP, and older scripts might not run on newer versions. Each script should include information about which version of PHP it requires. (FYI, if you are hosted here, you are using PHP version 5.1.2.)

PHP for easy page updating

Most people use PHP for one very useful function - include('filename.php');. This lets you import the entire contents of the file or your choice into your current document.

This can be useful for many things like keeping headers, footers, and menus separate from your page content so that you don't have to change dozens of different pages every time you want to update your menu. Use an include any time a large chunk of text/HTML is repeated on every single page.

Try this example: Create a file with only these four lines and save it as "menu.php".

<a href="">link1</a>
<a href="">link2</a>

Create another file with these lines and name it "includetest.php":

Here is one menu.
<?php include('menu.php'); ?>
Here are two more menus.

Now upload both menu.php AND includetest.php, and visit includetest.php in your browser. You should see something like this: includetest.php sample

As you can see, you can include the same file as many times as you like, anywhere in your document. You can also include several different files in the same document, such as a menu, HTML headers, HTML footers, anything you like!

PHP finds your full path!

The biggest problem people seem to have with using PHP is that they're not sure what their full path is. This is completely different than the absolute path used for URLs (, for example) and isn't always the easiest thing to figure out.

Stick this little snippet of code somewhere on your PHP page:

echo dirname(__FILE__);

Upload the file and look at it using your web browser. Wherever you inserted the code, you should see the full path leading to the file you are viewing. Here's an example.


See that? Up there? You should see this:
I know it looks the same, but I am running the PHP command. Trust me.

Compare it to the URL:

So the last part of the path is your domain/subdomain. Directly before that is your username. Mine's clavis. Before THAT is your webserver name - everyone hosted on shares the same one, .lafayette. And finally, all of this stuff is in the home directory on the server.

If you see anything AFTER your domain/subdomain name, that just means that you are inside a folder, and you should be able to confirm that easily by taking a look at the URL bar in your browser.

Again, running the code on your webspace will give different results. This is a GOOD thing. Just remember what each piece of it means and you shouldn't have any problems with PHP things like skin switchers or other fun tools.

How is this useful?
You can use the full path in your PHP include if your page has a lot of subdirectories or if you want to be sure the right file always gets included. It looks a bit intimidating but can save you a lot of trouble if your site is big and has a lot of files.

be confident!

This looks intimidating, but practice with it a little and you'll see how easy and useful includes can be! Don't be afraid of breaking the server or causing a problem. Testing out the code is the best way to understand how it works.

For more information about includes, please see the css/html/php help section of the links page.