| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
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. |
Advanced FunctionsThese tutorials are about PHP and its use for server-side Web programming. Advanced FunctionsDefault Parameter ValuesPHP 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.
Variable Length Argument ListsIf 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.
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 FunctionsAs 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 FunctionsPHP 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
$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. NestingIn 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.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|