PHP Tutorials – Functions (Part-2)
In this PHP Tutorial 2nd Part of PHP Functions you will learn about Variable parameters – func_num_args(), func_get_args(), func_get_arg($arg_num), Variable variables and variable functions, Some useful functions – phpinfo() and header()
Variable parameters
Sometimes you may not know the number of parameters to be passed for a function, for example if you want to make a reusable function that calculates the sum of some numbers, and you want it to be able to calculate any number of numbers, to do so, you need to use the variable parameters.
Before you know how to use the variable parameters, you need to know about some functions that are provided by PHP to investigate function parameters:
• func_num_args(): returns the number of passed parameters to this function.
• func_get_args(): returns an array of the values of all the arguments passed to this function.
• func_get_arg($arg_num): returns the value of a specific parameter, given the parameter position in the arguments list.
These functions make it easy to do the previous task, lets see how:
<?php
function sum() {
$sum = 0;
$args = func_num_args();
for($i = 0; $i < $args; $i++) {
$sum += func_get_arg($i);
}
return $sum;
}
echo sum(5, 7, 12);
?>
The code passes the values 5, 7, and 12 to the function to get the sum, the function defines a variable called $sum and initialized it by zero, this will be the container of the sum of the arguments, the function gets the number of the arguments by using func_num_args(), the then gets the value of each parameter using the function func_get_arg() using the parameter index, note that the first index is 0, not 1.
Variable variables and variable functions:
Variable variables is very useful feature, let’s see what does it mean by an example:
<?php
$x = 5;
$y = "x";
echo $$y;
?>
The expression $$y is first evaluated to $ plus $y, which equals x, so the first evaluation will return $x, which is evaluated to 5.
The same technique can be used to call variable functions.
Example:
<?php
function equal($x) {
echo "$x equals 10";
}
function not_equal($x) {
echo "$x does not equal 10";
}
$y = 10;
$z = ($y == 10? "equal" : "not_equal");
$z($y);
?>
$y equals 10, so $z will be evaluated to ‘equal’, so the statement $z($y) will be evaluated to:
equal($y), which will print 10 equals 10.
Some useful functions:
1. phpinfo(): used to output the installed php information, the information that can be obtained from this function are version, configurations, credits, installed modules, license, and many other information.
2. header(): used to put some variables at the html header that’s delivered to the client, the header(0 function must be used before anything is written in the page, because header is prepared first, if anything is written before the header function, it will return an error.
Example:
<?php
header("Location: http://www.google.com");
?>
This will write the following line to the header:
Location: http://www.google.com
When the browser reads this line, it will stop reading the rest of the page and open the URL specified in the Location header, this is the way that you use to redirect users depending on special cases.