[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
4. php expressions
5. php client side
6. php flow control
a. php conditionals
b. php adv conditions
c. php iteration
d. php functions
* e. php adv functions
f. php modularity
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Advanced Functions

These tutorials are about PHP and its use for server-side Web programming.

Advanced Functions

Default Parameter Values

PHP allows you to set default parameter values in the function definition. This allows the parameters to be assigned values for when a value is not passed in for them. The format looks like this:

function withDef($x, $y = 12; $z = 24) {
  [ ... statements ... ]
  }

When you use default values for parameters, they become optional and do not have to be included in the function call. The rules regardnig their use are pretty simple.

  • Parameters with default values must be declared after parameters without default values. Which is to say, you have to specify the required parameters before you specify the optional ones.
  • You cannot leave any blank parameters between the start of the list and the last one spcified. Every parameter up to the last one provided must have a value passed to it. Thus you should order arguments in the order of must common inclusion to least common inclusion.

Variable Length Argument Lists

If you don't know how many arguments you will need to provide, PHP does have methods for working with any number of arguments in a function.

To make use of them you create a function with no parameters.

function someFunc() {
  [ ... statements ... ]
  }

You then call the function with as many parameters as you want to pass to it. Within the function you make use of a set of system defined functions that access the parameters passed in and allow you to work with them.

func_get_args()
This function returns the arguments that have been passed to the function as an array. You can then walk through the array normally.
func_num_args()
This function returns the number of arguments that were passed to the function. Useful to test whether any have been passed at all.
function someFunc() {
  if (func_num_args() == 0) { return false; }
  [ ... statements ...]
  }
func_get_arg()
This returns a single argument for the arguments passed to the function. It takes as an argument the index position of the argument you want returned. Not that it is a separate function from func_get_args() and differs only by the absence of an S at the end.
function someFunc() {
  if (func_num_args() == 0) {
    return false;
    }
  else {
    for ($i =0; $i < func_num_args(); $i++) {
      $x = func_get_arg($i);
      [ ... statements ...]
      }
    }
  }

The only caveat to these functions is that they cannot be used as arguments to be passed to another function. You can write the results to a variable and use that.

Variable Functions

As with variable variables, PHP allows you to create variable functions.

A variable function is one whose name has been assigned to a variable and which is then called through that variable.

function fX($a) {
  [ ... statements ... ]
  }

$indirect = "fX";
$y = $indirect($x);

This allows you to dynamically determine function names to be executed from previous processing.

Anonymous Functions

PHP also allows you to generate anonymous functions, also called lambda functions. These functions do not have names. They are instead assigned directly to variables and called by the variable name. This allows you to create transient functions without worrying about having to think up a new function name each time you do.

To create an anonymous function, you use the create_function() function. It takes two strings as arguments. One represents the parameter list for the function, exactly as it would appear between the parentheses in a normal function definition. The other is a string the contains the actual body of the function. As you might it expect, it is best used with shorter functions.

$fX = create_function('$a,$b','return $a*$b;');

$z = $fX($x, $y);

Anonymous functions do have names, but they are randomly generated strings, which minimizes the chance of the function accidentally producing two functions with the same name.

The ability to create anonymous functions exist in PHP primarly for interacting with functions that expect other functions as their arguments. It makes the coding easier and most such functions are very contextual and tend to require variations if not within the same execution of a script than certainly between executions.

Nesting

In PHP, function definitions can be nested. Why is this? Perhaps because they can. More likely to make it more effectively support an object oriented environment.

When you define a function within another function it does not exist until the parent function is executed. Once the parent function has been executed, the nested function is defined and as with any function, accessible from anywhere within the current document. If you have nested functions in your code, you can only execute the outer function once. Repeated calls will try to redeclare the inner functions, which will generate an error.

PHP functions can also be nested inside other statement blocks, such as conditional statements. As with nested functions, such functions will only be defined when that block of code is executed. You can use this to get around the lack of overloading in PHP by allowing mutliple versions of the same function to be set up and then have an if statement to determine which one to actually define. Since a function can only be defined once, this will only work if the conditional is only executed once in the script.

[top]