Paris Graphics

PHP

also available:

Intro

Like many of my tutorials, I'm not going to cover everything there is to know about PHP here. There are many resources - books and online tutorials that do that. What I hope to do is give enough of an intro by going through some examples that you can then do your own research and learn more.

PHP is an open source "scripting" language that is used primarily for creating interactivity on web pages. By interactivity what I really mean is something happening on the webserver or database (in contrast to simple Javascript or Flash where the code is executed in the web browser). PHP is full fledged programming language that has variables, conditionals, arrays, classes and more. Nevertheless, a PHP program is often referred to a "script" - because it is generally executed via an interpreter at the time script is run as opposed to languages (like C for example) where code is compiled into an executable program. Interpreted languages such as PHP (and Perl for example) have advantages such as:

In order to follow along with this mini-tutorial or try out your own PHP scripts (or ones you find), you will need access to a webserver account with PHP installed. You will also need a text editor and an FTP client. Unless otherwise noted, PHP files are saved with the extension: .php

As mentioned above this is a quick overview of some aspects of PHP. Especially if you are new to programmig concepts like variables, arrays, conditionals, and so on you should look at some books and/or online tutorials. Here's a few recommendations:
The PHP Web Site - in particular the searchable documentation.
PHP 101: PHP for the Absolute Beginner
PHP Cheat Sheet

Including PHP in your XHTML file

When you write PHP code it will most likely be one of these cases:

In either case, the way to write it is like this:

<?php

php command goes here ;

?>

That is, you begin with

<?php

and then have any number of php commands with each command line ending in a semicolon

command one;

command two;

etc

and then you close with

?>

 

Let's try an example of PHP mixed with some HTML

<html>

<head><title>Getting the Date</title></head>

<body>

<p>Today is:

<?php echo date("m/d/Y"); ?>

</p>

</body>

</html>

To see this in action, click here.

What's going on:

Note that if you "view source" the file we just created, you would see all html - no php. The PHP interpretor has executed the commands and functions and placed the results into the page.

 

The echo command is very simple but used frequently - so let's look at some typical ways it's used.

In the example below I will use C++ style comments within the code. PHP also supports C style comments (/* like this */) and shell style comments (# like this).

<html>

<head><title>Echo</title></head>

<body>

<?php

// echo some html text

echo "<p>Hello World!</p>";

// echo some variables

// we haven't talked about variables yet but basically they are a named place to store something

// variables have a dollar sign prefix

$color = "red";

$size = "large";

echo "<p>The car is $size and $color</p>";

//sometimes you want to print a whole block of text

//this can be done using a "here document" which works like this

echo <<<BLOCK

echo <<<BLOCK
<p>
This is a block of text which might have variables or html. <br />
It might be a few sentences or paragraphs.<br />It ends with the same word that you started the block with - but it must come on the very next line after your text.</p>
BLOCK;

// there's nothing special about the word BLOCK you could use anything.

?>

</body>

</html>

You can see this in action here.

 

A Simple Form Processing Example

Since I brought up variables in the last example, let's use them to process a simple form. First let's create a form in an html page. Something like this will do:

<form action="simpleForm01.php" method="post">
<div>
<span><label for="firstname">First Name:</label></span>
<span><input type="text" name="firstname" title="Text input: First Name" id="firstname" size="20" /></span>
</div>
<div>
<span><label for="lastname">Last Name:</label></span>
<span><input type="text" name="lastname" title="Text input: Last Name" id="lastname" size="20" /></span>
</div>
<div>
<span><label for="food">What you would like for dinner:</label></span>
<span><input type="radio" name="food" value="tofu" /> Tofu
<input type="radio" name="food" value="beans" /> Beans
</span>
</div>
<div>
<span><input type="reset" name="Reset" value="Reset" />
<input type="submit" name="Submit" value="Submit" />
</span>
</div>
</form>

You can see the form here (and if you really can't resist, you can fill it in - otherwise wait ;-)

The following PHP script (which if you are following the above example should be named simpleForm01.php) will take the values from the form and output them with some additional text.

<?php

$fname=htmlspecialchars($_POST["firstname"]);
$lname=htmlspecialchars($_POST["lastname"]);
$food=$_POST["food"];

echo "<p>Hello $fname!</p>";
echo "<p>Your last name is: $lname</p>";
echo "<p>Your food choice is: $food</p>";

?>

Take a look for a moment and then go ahead and submit the form. What's happening...

At the top we set up our variables. There is a special array in PHP called a "super global" that looks like $_POST["someVariableInYourForm"] that gets the values of the variables which are POSTED in a form. Remember that forms can be submitted via POST or GET actions. There are good uses for both. However, in general for any form that sends user informatoin should use POST. This is because POST will send the "invisiblely" whereas a form posted with GET will append the user info to the URL. For example, searching for Nullsleep on Flickr gives a results page with the URL:
http://www.flickr.com/search/?q=Nullsleep&m=text

Had this been user info for a form going into say a database, someone could easily change values by modifying the URL.

The next thing to notice about the code above is that we are using the PHP function htmlspecialcharacters. We do this so that if someone types in HTML or JavaScript code, it will be converted to character codes and not interpreted. Go ahead and try it if this doesn't make sense - type in some HTML and see what happens.

We can improve on this script in several ways some of which are covered in Part 2