Login |  Register 
After taking your course, my knowledge of PHP has ballooned!

User defined functions

A function can be defined either inside or outside of a class. The main purpose of a function is to collect together a repeated group of statements that may appear either now or in the future multiple times.

Then instead of having these groups of statements throughout the code the function that contains this group of statements can be called.

This reduces code duplication which makes it easier to maintain the code, as the code doesn't need to be changed in multiple places through multiple files! Furthermore it helps modularize the code which means the processing is split up into sections so that we don't need to concern ourselves with how the processing is done as a whole, but instead how a function is coded to perform its one task.

Functions can then be called and there results combined to perform much more complex tasks

Any variables (ie values that can change per call) can be put into function parameters so that the function can be configured for use for each call. These function parameters can also have the types that they except limited.

Any valid PHP code, even functions and class definitions can appear inside a PHP function.

//The delete function can't be called at this point as it has not been created yet.
function makeUser($username, $password)
 function deleteUser()
 $user = new User($username, $password);
makeUser('tom', 'pass');
//The deleteUser function can now be called as its been created.

All PHP non-class functions are global so that once created they can be used in any file that references that function. However as shown above a function can't be called until it has been created.

The variables $username and $password above are function parameters that are variable values passed to the function. You can pass any number of parameters to the function. The parameters can be any valid PHP variable so objects, arrays, or literals.

Function names follow the usual PHP rules so they can start with any alphabetical character or an underscore followed by any number of letters, numbers or underscores.

Returning a value

Functions in PHP can return a value by using the return keyword. The return simply indicates that the function will output that value.

function myName()
 return "Tim";
echo myName();
//This prints Tim

The code above shows how to use a return the myname function echo's Tim.

An alternative to using return is to use references this is when a passed in variable will keep any changes that occur to it inside the function and these changes can be used after the function has finished processing. A parameter is marked as a reference parameter by putting a & in front of the parameter decleration.

function myNameAndAge(&$age)
 $age = 29;
 return "Tim";
$myAge = 10;
echo myName($myAge);
//This prints Tim
echo $myAge;
//This prints 29

Recursive functions

A function that calls itself is known as a recursive function. This is especially useful for navigating tree structures. However recursive functions must be used carefully as recursion with 100-200 recursion levels can cause the current script to terminate!

function findIt($name, $multiDimensionalArray)
  foreach($multiDimensionalArray as $item)
   if ($item == $name)
    return true;
   if (is_array($item))
    return findIt($name, $item);
  return false;

As you can see above a recursive function only calls itself if some condition is met. The above function searches an array for a name and if it finds that a item is another array it calls itself again to search that. The return when findIt function calls itself is important as this ensures that the recursive function returns what it finds to the calling function.

Comments to date: 2. Page 1 of 1. Average Rating:
Ldfgdfhdfh   1:41pm on Monday, May 13th, 2013 
Thank you for present hihlgy wonderful informations. Your internet is fantastic, I am happy by the material which you have on this blog. It exhibits how very well you have an understanding of this subject. Bookmarked this page, will arrive again for ... read more »
Michael   1:55am on Thursday, December 2nd, 2010 
Enjoying the tutorial! Just noticed that your not W3C XHTML compliant... so close though!

security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38