Introduction to PHP


PHP:

PHP stands for “Hypertext Preprocessor.” Duh why is it “PHP” then? Because when it was first invented, it stood for “Personal Home Page.” Then it changed radically into something else, but they kept the extra P for no reason anyone understands.

Anyway, “Hypertext Preprocessor” is a pretty accurate description of what it is. Everything else that we’ve done thus far -- plain HTML, CSS, frames, tables, JavaScript -- has all been stuff that you write for a web browser to “parse” (read and understand) directly; that’s why you can view your web pages on the computer you’re using, rather than having to upload them to a web server.

PHP is different. PHP code is not something that a browser can interpret; it has to be PREPROCESSED. When we put PHP code in pages, we can no longer preview them locally -- we have to upload them to a real webserver first, and that webserver has to have the PHP interpreter installed. www.whoopis.com does have it, and PHP is very popular, so most webservers now have it too -- but not all of them. You’ll know right away if you’re using a webserver that doesn’t have it, because you’ll see the “raw” PHP code, rather than the preprocessed results.

This is an important concept. When we write HTML or JavaScript, we’re sending something directly to a web browser which then displays it. When we write PHP, we’re telling the webserver how to send something to a web browser which then displays it. There’s an extra layer. Your web browser never knows that there was PHP code involved in the page it sees -- all it sees are the results of whatever the PHP code told the webserver to do.

Pages you make that use PHP need to have a filename that ends in .php instead of .html. This is how the webserver knows to run your page through the preprocessor before sending it to a browser. PHP is extremely flexible in that you can switch back and forth between regular HTML and PHP code within the same page. The PHP code has to be inside a special sort of tag, which starts with

<?php

and ends with

?>

And every statement in PHP needs to end with a semicolon. Here’s an example:

<html>
<head>
<title>A Boring Page</title>
</head>
<body>
<p>Hi! This is regular HTML, but we’re about to do some PHP...

<?php
$sum = 2 + 2;
echo “<p>The sum of 2 plus 2 is $sum”;
?>

<p>And now we’re back to regular HTML.

</body>
</html>

Once we’ve uploaded this page to a PHP-enabled webserver, and then surf to it, we see:

Hi! This is regular HTML, but we're about to do some PHP...

The sum of 2 plus 2 is: 4

And now we're back to regular HTML.

NOW: The rather surprising part. What do you expect to see when you now view the source of this web page?

<html>
<head>
<title>A Boring Page</title>
</head>
<body>
<p>Hi! This is regular HTML, but we're about to do some PHP...

<p>The sum of 2 plus 2 is: 4
<p>And now we're back to regular HTML.

</body>
</html>


Wait a minute. Where’s the PHP?

It’s been preprocessed by the PHP module of the webserver, and the results are just regular HTML. Note how my “echo” statement included a <p> tag; I was telling PHP to send some HTML, along with the results of my addition of 2 and 2.

Okay, so adding 2 and 2 is not real exciting. What IS exciting, for a web designer, is the concept of “server-side includes.” Remember how CSS allowed you to define the aspects of the appearance of your webpage elements -- fonts and colors and borders and such -- and then repeat those elements easily, with just a little bit of HTML? You still had to write the content, and if you had something like a copyright statement or a banner that you wanted to put on every page, you had to copy and paste that same chunk of code over and over. Then, if you decided to change it later, you had to go through and change it on every page -- annoying. Well, PHP allows you to do with content, what CSS let us do with appearance.

First we make a file to define the repeating stuff. We’ll save it as “includes.php”:

<?php

$myheader = ‘
<center>
<img src=”banner.jpg” alt=”My Site Banner” border=”0”>
<p>Welcome to my site!</p>
</center>
‘;

$myfooter = ‘
<center>
&copy; Copyright 2005 by me.
</center>
‘;

$mynavbar = ‘
<p><a href=”links.php”>Links</a>
<br><a href=”updates.php”>Updates</a>
<br><a href=”other.php”>Other stuff</a>
‘;

?>

Note the “punctuation” again -- like with JavaScript, it matters. Now, the webpage:

<html>
<head>
<title>A page with includes</title>
</head>

<body>

<?php
include(‘includes.php’);
echo $myheader;
?>

<p>Here’s my main content, which is different on every page.

<?php
echo $mynavbar;
echo “<p><hr>”;
echo $myfooter;
?>

Now, any time I decide to add another item to my navigation bar, all I have to do is edit the single “includes.php” file, and since all my pages already use that “$mynavbar” variable, the changed nav bar will appear in all my pages. It’s a HUGE shortcut.

You can make functions in PHP, just like with JavaScript, and in my opinion they’re easier to write and understand. See if you can make sense of this one:

<?php
function makeTitle($specific) {
echo “<title>My Web Site -- Current Section: $specific”;
}
?>

<html>
<head>
<?php makeTitle(“Main”); ?>
</head>
<body>
<p>blah blah...
</body>
</html>

PHP pitfalls:

1. Single quotes vs. double quotes. Look carefully at this:

<?php
echo “<p>This is a “nice” day.”;
?>

This won’t work. Can you see why? PHP’s “echo” statement treats double quotes as the beginning and end of the thing that it’s supposed to echo. If we then try to use double quotes inside that echo statement, PHP will think that we want it to echo

<p>This is a

because it stops at the first set of quotes. But then, there’s all this other stuff:

nice” day.”;

and it doesn’t know what to do with that, so it gives up and we get an error. So, what if you NEED to put double quotes in your echo statement? There are two ways, and each has its advantages and disadvantages depending on what else you’re doing with that piece of code.

The first way is to escape the inner quotes, by using a backslash (the one above the return key, NOT the one on the question-mark key). To PHP, a backslash means “don’t treat this part as code.”

<?php
echo “<p>This is a \“nice\” day.”;
?>

The second way is to use single quotes for the echo statement, like this:

<?php
echo ‘<p>This is a “nice” day.’;
?>

HOWEVER -- this is key -- single quotes change how the echo statement treats variables. If you’re not using any variables, then don’t worry about it. But if you are, then you need to be careful. Remember the function above? What if we wanted to use double quotes in there too? It gets a little weird.


<?php
function makeTitle($specific) {
echo ‘<title>My Web Site -- “Current” Section: ‘ . $specific;
}
?>

The period in PHP (and many other languages) means “glue these two things together.” So basically, we’re telling the echo statement to glue two things together:

‘<title>My Web Site -- “Current” Section: ‘

and

$specific

If we just wrote it like

echo ‘<title>My Web Site -- “Current” Section: $specific’;

then we’d get something we didn’t want, which would be the word “$specific” LITERALLY in the page title. The use of single quotes in the echo statement causes echo to treat everything inside literally.

Switching from double quotes to single quotes vs. escaping inner quotes is completely a matter of what you find less confusing. When I am using PHP to echo a big chunk of HTML, I tend to use single quotes; when I am just echoing a small thing, or a variable, then I’ll use double quotes. Here’s why: As you know, many HTML tags use double quotes! See how annoying this would be if I used the backslash-escape method:

echo “<p><table border=\”0\” cellpadding=\”5\” cellspacing=\”5\” width=\”600\”>
<tr bgcolor=\”#ffffff\”><td><font face=\”Arial\” size=\”3\”>blah</font></td></tr></table>”;

vs. the easier-to-read

echo ‘<p><table border=”0” cellpadding=”5” cellspacing=”5” width=”600”>
<tr bgcolor=”#ffffff”><td><font face=”Arial” size=”3”>blah</font></td></tr></table>”;

There’s MUCH more to PHP, but these are the most useful (I think) parts that you’re most likely to want to use. For more info, Google “PHP tutorial” -- that’s how I started.


Main