PHP Tutorial

I used to have a PHP tutorial here, and it was somewhat heavily downloaded. It needed a revision that never happened. Anyway, it's lost, so here's a new one.

Writing tutorials for newbies is fairly difficult. The subject matter is simple, but the exposition needs to be deliberate. Additionally, written instructions are linear, and follow a single path through the instruction. The problem is, we don't learn in a linear manner; we learn through repetition and by trying different things. So there's a mismatch between how we learn, and how we're taught.

The short solution is for learners to try different tutorials, and for tutorial writers to expose the material using different examples or in a different order.

Here's another take on learning PHP.

(Planned path: Reading Data, Defining New Functions, Reading Lots of Data and Arrays of Arrays, Split, Looping Over Arrays, Libraries of Functions, Writing Data, Data Formats (serialized, base64, CSV, tab delimited), Databases, Regular Expressions, Data Validation, Objects as Data Structures, Objects for Program Organization and Encapsulation, Looping Over Directories, A Database Abstraction Class, A Directory Abstraction Class.)

Other Tutorials

For starters, here are some links out to some existing tutorials.

W3Schools PHP Tutorial

TizTag's Tutorial

Webmonkey's Tutorial

Zend's tutoral

Evaluation and Variables


<p>
<php echo 1; ?>
</p>

<p>
<php echo 1 + 2; ?>
</p>

<p>
<php echo 2 * 3; ?>
</p>

<p>
<php echo 10 / 2; ?>
</p>

<p>
<php echo 10 / 3; ?>
</p>

<p>
<php echo 1 + 2 * 5; ?>
</p>

<p>
<php echo ( 1 + 2 ) * 5; ?>
</p>

<p>
<?php
    $a = 10;
    echo 2 * $a;
?>
</p>

<p>
<?php
    $a = 10;
    $b = 2;
    echo $b * $a;
?>
</p>

<p>
<?php
    echo "Hello, World";
?>
</p>

<p>
<?php
    $a = "Rose";
    echo "Hello, ";
    echo $a;
?>
</p>

<p>
<?php
    $a = "Rose";
    echo "Hello, " . $a;
?>
</p>

<p>
<?php
    $a = "Rose";
    $b = "Hello, " . $a;
    echo $b;
?>
</p>

<p>
<?php
    $a = "Rose";
    $b = "Hello, $a";
    echo $b;
?>
</p>

<p>
<?php
    $a = "Rose";
    $b = "Hello, \$a";
    echo $b;
?>
</p>

<p>
<?php
    $a = 100;
    $b = "The price is \$$a";
    echo $b;
?>
</p>

<?php
    $a = 100;
    $b = "The price is \$$a";
    echo "<p>";
    echo $b;
    echo "</p>";
?>

<?php
    $a = 100;
    $b = "The price is \$$a";
    echo "<p>$b</p>";
?>

Arrays, Passing Values, the $_GET[] Variable

In the previous chapter, we explored evaluation, evaluation order, variables, numbers and strings.

In this chapter, we explore how to send input to our short programs.

The most basic way to pass data from the web browser into our program is through the URL's query mechanism.

You've seen URLs that look like this:

http://foo.bar.com/index.php?id=100

The stuff to the right of "?" is called the query. The query is made up of pairs of names and values, separated by =; the pairs are separated by "&". Parameters are like variables, in that they assign values to named storage. In the above URL, the value of "id" is "100".

In PHP, these parameters are passed to a PHP script through the $_GET[] array variable.

Before discussing $_GET, let's quickly discuss arrays.

An array is an ordered list of values. You can assign an array to a variable, like this:

<?php
$a = array( "John", "Rose" );
?>

<?php
$a = array( "John", "Rose" );
echo $a[0];
?>

<?php
$a = array( "John", "Rose" );
echo $a[1];
?>

An associative array is a variation on the array, where, instead of referring to each element by number, you use a name.

<?php
$a = array( "host" => "John", "guest" => "Rose" );
echo $a["host"];
?>

<?php
$a = array( "host" => "John", "guest" => "Rose" );
echo $a["guest"];
?>

<?php
$a = array( "host" => "John", "guest" => "Rose" );
echo $a["Guest"];
?>

<?php
$a = array( "host" => "John", "guest" => "Rose" );
echo "<p>Host: $a[host]</p>";
echo "<p>Guest: $a[guest]</p>";
?>

Now, we can discuss the special variable $_GET[].

$_GET[] is an array that contains the parameters in the URL's query. Thus, if the URL is:

http://foo.com/test.php?id=100&filter=91770

Then the value of $_GET[] are:

$_GET["id"] = 100;
$_GET["filter"] = 91770;

Here's a script that will add two numbers:

<?php
$a = $_GET["a"];
$b = $_GET["b"];
echo $a + $b;
?>

<?php
$a = $_GET["a"];
$b = $_GET["b"];
echo "$a + $b";
?>

<?php
$a = $_GET["a"];
$b = $_GET["b"];
echo "$a + $b = " . ( $a + $b );
?>

HTML Forms and $_GET[]

As clever as it is to alter the URL to pass values to your script, the normal way to pass values is through a web form.

Here's some HTML that will display a form with two fields.

Here's some PHP code to do something with that form.

Here's a script with the two parts combined.

Here's another script, but one which displays the previously entered value in the form.

POST is another technique, similar to GET, but different.

HTML Forms and $_POST, a Simple Templating Script

This page will describe the difference between GET and POST, and the typical uses for each.

Generally, you use GET when you want the user to be able to bookmark the page. You also use it when retrieving data from a database (we'll get to that later).

You use POST when you're writing data to a database (again, we'll discuss this later). You don't want people bookmarking that kind of data, because it would quickly lead to repetitive data being recorded.

Calling Functions, Writing Files