php 101

Functions

So far we've only been using a single built-in PHP function (echo()), but there are hundreds of them available. Since PHP is built for making web sites, it's not surprising that it includes a lot of text-manipulation functions. The official PHP documentation lists every single function built into the language, and there's a whole section on text manipulation functions alone. Let's try a few, starting with strtoupper(), which simply converts a string to upper case:

You should have gotten back:

MARY HAD A LITTLE LAMB.

There's a similar strtolower() function which works like you'd expect it to. The basic usage of a function is this:

Most functions take at least one argument (arg1), which is usually the name of the thing you want to do work on, which could be a string, a variable, a number, or whatever is appropriate. Additional arguments (arg2, arg3...) can be used to tell the function how to act. To illustrate that, let's try out a function that takes more than one argument. The wordwrap() function can occasionally be useful, and takes arguments for width (how wide do you want your text lines to be?) and the line-ending (in this case we'll insert <br /> tags after each line.)

The script returns:

The quick brown fox<br />
jumped over the lazy<br />
dog.

Tip: The official documentation for each function on php.net can be pretty terse. If you have trouble understanding the abstract function definitions, skip down to the examples section include on each page - it's usually pretty easy to figure out how to use the function from reading the examples and discussions that follow.