# Using PHP For Interactive Web Pages - Additional Features

Author(s):
Andrew G. Bennett

In the best tradition of open source software, PHP has accumulated an enormous set of functions that implement features from a wide variety of other tools and languages. The Function Reference section of the online documentation has 108 chapters on different varieties of functions. For example, there are functions supporting over a dozen different databases. There is support for regular expressions in both POSIX and Perl Compatible varieties. The BCMath library for arbitrary precision arithmetic is included. There is support for XML data. And of course, like any modern language, PHP allows you to define, create, and use objects. With such a wide range of features, it is important to remember that you don't have to use anything you don't want. While PHP provides the option to use whatever tools appeal, you can do quite well sticking to just the basics.

There are two special features of the language besides the enormous function set that are worth noting, both relating to PHP's flexibility in dealing with strings.

First, PHP lets you use strings in many places where other languages restrict you to integers. For example, you can build arrays with string indices (associative arrays) in addition to the usual integer-indexed arrays. There are a wide variety of array functions available to retrieve and sort both values and keys (indices) from associative arrays. These arrays are particularly useful because the language contains built-in associative arrays of form variables ($HTTP_POST_VARS,$HTTP_GET_VARS) that can be used to design a generic form response system that doesn't depend on knowing the specific form elements in advance. If you are used to using associative arrays and regular expressions in PERL, you can use a lot of the same techniques in PHP -- though typically in more readable form. PHP also allows you to use strings as the cases of a switch statement.

The second special treatment of strings by PHP is illustrated by the following code snippet.

<?php
$a="hello"$b="a";
$c=${$b}; ?> This code assigns the value "hello" to the variable$c. The ${} construction refers to the variable whose name is given by the string within the brackets. The brackets can even contain a function, as long as it produces a string that forms a valid variable name. Similarly, you can use a string to invoke a function. For example, the code snippet <?php function triple ($a) {
return 3*$a; }$f="triple";
$x=7;$y=$f($x);
?>

assigns the value 21 to the variable \$y. These techniques give you much of the power of pointers in C. Unfortunately, they also give you the power to create many (though not all) of the bugs associated with pointers.

Andrew G. Bennett, "Using PHP For Interactive Web Pages - Additional Features," Convergence (December 2004)

## JOMA

Journal of Online Mathematics and its Applications