Page 95 - Open Soource Technologies 304.indd
P. 95
Unit 4: Functions
Notes
Let us discuss the solution provided.
$result = array($add, $subt);
The reason we use array is because array simply takes variables and puts then into a single
structure and assigns it to a single value like the above example. So when we want to echo both
of the returned value, we call them like so:
$result_array = add_subt(10,5);
echo “Add: “ . $result_array[0] . “<br>”;
echo “Subt: “ . $result_array[1] . “<br>”;
The number in the bracket [0] and [1] indicates the $add and the $subt variable from the function
attributes that we called earlier: array($add, $subt); So [0] for additional and [1] for subtraction.
So that’s the way that we can pass multiple results out of the function. So if we need to pass
something more that just one value, we can do it with an array.
Create a PHP code using <html> ,<head> and <title> tag in the return value.
4.6 Variable Functions
PHP supports the concept of variable functions. This means that if a variable name has parentheses
appended to it, PHP will look for a function with the same name as whatever the variable evaluates
to, and will attempt to execute it. Among other things, this can be used to implement callbacks,
function tables, and so forth.
Variable functions would not work with language constructs such as echo(), print(), unset(),
isset(), empty(), include(), require()and the like. Utilize wrapper functions to make use of any of
these constructs as variable functions.
Example: Variable function.
<?php
function foo() {
echo “In foo()<br />\n”;
}
function bar($arg = ‘’)
{
echo “In bar(); argument was ‘$arg’.<br />\n”;
}
// This is a wrapper function around echo
function echoit($string)
{
echo $string;
}
LOVELY PROFESSIONAL UNIVERSITY 89