Last week we began a discussion on the language PHP with a small digression on its name. According to many of those who see PHP as a way of life, the name is a recursive acronym that stands for “PHP: Hypertext Processor.”
We need to correct this. Adam Gaffin, the executive editor of Network World Fusion and chief smart aleck, pointed out that PHP originally was called “Personal Home Page” – a name so unsexy that a touch of revisionism was a must.
Anyway, last week we discussed the background and attributes of the language, and we concluded with the usual “hello world” example.
This week we’ll take a deeper look at PHP programming. We won’t dwell too much on the basics of PHP, as much of the nuts and bolts are similar to Microsoft’s Active Server Page (ASP) technology.We’d recommend a rather useful review by Jalal Pushman that compares PHP with Visual Basic, ASP, Perl and Java (for more information, see Network World Fusion’s PHP resources page).
PHP variables – which are always prefixed by a dollar sign as in $avar or $acounter – do not have to be declared before you use them; they automatically are declared when a value is assigned. For example, the variable $name is created by assigning a string value and is, not surprisingly, of the type string :
$name = "Gearhead.";
Note the semicolon – it is required at the end of every statement. Also note that variables are case-sensitive (that is, $name and $Name are different variables) but everything else, such as PHP keywords and function names, are not.
Unfixed variables
PHP variables also are “loosely typed,” that is, their type (integer, double, string, Boolean, object or array) isn’t fixed and can be changed with wild abandon. This means that you can do things such as prefix single-line comments with # or // and frame multiline comments with /* ... */:
$avar = "The year is";$avar = ($bvar . $avar);
# $avar is created as a string type
$bvar = $avar;
# $bvar is created as string type by being assigned the value of $avar
$avar = 2003;
# $avar now becomes an integer by being assigned an integer value
/* the value of $avar is forced to be a string type by the string concatenation operator and the result of the concatenation with $bvar is assigned to $avar which forces the type of $avar to become a string again. Whew. */
Now check this out:
print 2003 + "2003";
The addition operator converts the string “2003” to an integer, adds it to the integer 2003 and passes the result, the integer 4006, to the print function, which in turn converts the value to a string and outputs it. If you need to you can ‘force’ the type of variable with a “cast operators”:
$avar = 2003;
$bvar = (string) $avar;
Or you can use settype():
$avar = 2003;
settype($avar, “string”);
$bvar = $avar;
The difference between these last two examples is that the first leaves $avar as an integer and the second forces the variable to become a string.
An important thing to remember about PHP is that there are three operations that you need to distinguish between: assignment, equality and identity. Each has its own operator: Assignment is “=” (make the variable on the left equal to the variable on the right); equality is “==” (test the variable on either side to see if they are equal in value); and identity is “===” (test the variable on either side to see if they are equal in value and type).
The distinction between “=” and “==” is crucial, as the following code will not, as you might have expected, test the variables to see if they are equal:
if($avar = $bvar)
This will evaluate to FALSE if $bvar is an empty string, the string “0” or the integer 0; otherwise it will evaluate to TRUE. The reason is that PHP first evaluates “$avar = $bvar” and assigns $avar the value of $bvar (whatever that is) and then tests $avar, the result of the first operation! Yep, it’s odd but you’ll get used to it quite quickly.
PHP is really good at string manipulation. See the article by Nathan Wallace on PHP’s string handling for a good primer.
Next week, PHP’s object model. Classify yourself to gearhead@gibbs.com.




