Chapter 2: What Is PHP?

Google

What does PHP look like?

First, the file names all end in ".php." You can take any HTML file and make it a PHP file just by changing the extension from ".htm" or ".html" to ".php." For example, if you have a web page called index.html and you rename it to index.php, it's now a PHP file. Since you haven't actually put any PHP code in it yet, it will still display just like a regular HTML file. PHP files can output HTML without doing anything extra.

Second, they usually have some PHP code in one or more blocks. A block of PHP code starts with <?php and ends with ?>. In some cases, it is possible to begin a PHP block with the short tag <?...but you should never do that! Here's why:

There's a configuration option in PHP that controls whether the short tag will be recognized. More and more web hosts are setting this option to "off." For your scripts to be usable on the greatest number of servers, use the <?php tag. This is very important if you plan to produce any PHP scripts and distribute them. Using the short tag may make a script unusable on your own server!

PHP blocks can be very short or they can be dozens of lines long. It all depends on what the PHP block needs to do. Here's a small example of a PHP block:

<?php
error_reporting(E_ALL);
$handle = fopen($file, "r") or die ("'$file' could not be opened\n");
$contents = fread($handle, filesize($file));
fclose($handle);
$contents = strtolower($contents);
$list = explode("\n", $contents);
sort($list);
$handle = fopen($file, "w") or die ("'$file' could not be opened\n");
fwrite($handle, "$list[0]\n") or die ("Couldn't write to file");
}
?>

A PHP file doesn't really have to have any HTML in it at all. It might just do its thing and then load an HTML or PHP web page.


Some cool features of PHP

Arrays: An array is just a list of items with a numeric index. Say what? OK. A variable just holds one item(regardless of its size or type) but an array can hold more than one item. Items in this list are numbered; that is, they have what's called a subscript with a number in it that you can use to get to a particular item in the list.

An array variable looks just like an ordinary variable:

For example, $IsNotAnArray might be an array and $IsAnArray might be a variable. See what I mean?

PHP can figure out which variables are "scalar" (one item) and which ones are arrays. It's a little hard to explain how it knows the difference. Just take it for granted that it does.

Putting stuff into an array(also called initializing the array) is simple. You can do it all at once, like this:

$monthName = array(1=>"January", 2=>"February",3=>"March", 4=>"April",
5=>"May", 6=>"June", 7="July", 8=>"August", 9=>"September", 10=>"October",
11=>"November", 12=>"December");

This creates an array with 13 elements numbered from 0 to 12. The subscripts or index numbers (1 to 12) are put in at the same time as the array elements (January, February, etc.). Arrays are normally indexed starting at element 0. Here, element 0 is created automatically. We don't care about it; we expect to look up the name of a month by using its usual number.

If you're initializing an array and you want the indexes to be in sequence (0,1,2,3...) you don't even have to put the index numbers in the line that creates the array. PHP will just number each element for you, beginning at zero.

$monthName = array(1=>"January", "February","March", "April");

Or, you can add elements to the array one at a time like this:

$monthName[1] = "January";
$monthName[2] = "February";
$monthName[3] = "March";

OR

$monthName[ ] = "";
$monthName[ ] = "January";
$monthName[ ] = "February";
$monthName[ ] = "March";

Note: If you didn't include an empty element first, January would have become element 0 in that last example.

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey