Home Tutorial PHP Lesson 12 - Using functions

MSN Random Online

Lesson 12 - Using functions PDF Print E-mail
Written by Administrator   
Thursday, 15 February 2007 17:58

Lesson 12 - Using functions

I've explained what a function is in lesson 3; now let me tell you how functions work from the PHP point of view.  You use functions when you have code that repeats.  Anytime you find yourself repeating a fair chunk of code, you can change it to a function saving yourself a lot of time typing, and a lot of space for the code.

PHP is built up of several functions.  For example, this function allows you to convert a string to upper case:

$string = 'this is a phrase';
$string = strtoupper($string);
echo $string; //prints "THIS IS A PHRASE";

strtoupper() is the function.  What it does is convert each character in my string to upper case characters.  This function saves you space because it automatically converts each character in the string to upper case for you.  If we look in the PHP documentation, this is what strtoupper() (http://ca3.php.net/manual/en/function.strtoupper.php) will tell us:

-The first line gives you the name of the function - "strtoupper"
-The second line tells us what versions of PHP handle this function - "(PHP 3, PHP 4)"
-The third line gives a very basic definition of what the function does - "strtoupper -- Make a string uppercase"
-The fourth line (and everything after) explains how the function works and gives definitions - "string strtoupper (string string)".

Let's take a look at how the function works:

string strtoupper (string string)

This says that when you use the "strtoupper" function and feed it a string, it will return a new string.  In other words: "string = strtoupper(string)".  You can read the PHP documentation on this function if you need examples.

The information you feed into a function are called "arguments".  The information a function can take are called "parameters".  These two words are sometimes used interchangeably.  When you define a function you define its parameters, but when you use a function, you give those parameters actual arguments.

Another very common function is the sort() function.  What this does is sort an array for you.  Let's look at the PHP documentation to see how to use this particular function (http://ca3.php.net/manual/en/function.sort.php).

void sort ( array array [, int sort_flags])

This function does not return any value (void), so you can't use assignment with this function.  In other words: "sort(array,int)".  The sort function takes an array as the first argument, and takes an optional second argument (that's what the [ and ] brackets mean, optional) which is an integer.  Reading the documentation a little more, we discover that the first argument is the array we want to sort, and the second optional argument allows us to sort the array in a particular way depending on the type of information we have in the array.  Let's look at a code example:

$array = array('b','a','q','z','y','a','c'); //let's define an array we want to sort
sort($array); //let's feed the array (via an argument) to the sort() function, and leave the optional argument blank
foreach ($array as $element) { echo "$element\n"; } //let's print out the array

That will print:


Read the PHP documentation and look for functions that might interest you.  Functions are named like actions (not quite verbs, but close).  You can find the functions (organized by type) at http://www.php.net/manual/en/funcref.php.  You can also find a full list of the functions at http://www.php.net/quickref.php.  To sum up everything above, you feed information to a function, it does a few things with the information you feed it, and it gives you new information back.



Top Members

No top members yet.