Arguments PHP

When declaring a function, you may declare arguments inside the parentheses, each separated by a comma. The arguments must be preceded by a dollar sign. They become variables inside the function. When the function is called, it expects values to be passed that will fill the arguments in the order declared.

Arguments, by default, copy the passed value into the local variable. If the variable is preceded by the & operator, the variable instead becomes an alias for the passed variable.This is commonly referred to as a variable reference. Changes made to referenced variables change the original.

To demonstrate this idea, imagine we wanted a function that stripped commas from numbers. That way if we got something like "10,000" from an input field we would know it was ten thousand, not ten. We could build the function by passing a string and returning it with the commas removed. But in this case we want to just pass the variable and have it be changed. demonstrates this functionality.

It is also possible to make an argument optional. Many built-in functions provide this functionality. The date function is one you should be familiar with by now. You can pass one or two arguments to date. The first argument is the format of the return value. The second argument is the timestamp, a date expressed in seconds since January 1, 1970. If the second argument is omitted, the current time is used.

You do this in your own functions by providing a default value using the = operator immediately after the argument. The right side of = is a literal value that the variable will be assigned. See Since arguments are matched up left to right, you must provide a default value for every argument after the first with a default value.

Passing Arguments by Reference

<?
function stripCommas(&$inputString)
{
$inputString = ereg_replace(",", "", $inputString);
}
$myNumber = "10,000";
stripCommas($myNumber);
print($myNumber);
?>

You may set an argument to be unset by default by making it equal to NULL, a special constant. demonstrates this functionality.

Other than named arguments, you may also access arguments by their position using three functions, func_get_arg, func_get_args, func_ num_args. You may either fetch one argument at a time using func_get_arg, or fetch them all as an array using func_get_args. To find out how many arguments were passed, use func_num_args. There is an implication lurking here. Calling a function with a number of arguments different from the prototype is not an error unless you write your function that way.

Arguments with Default Values

<?
function printColored($Text, $Color="black")
{
print("<FONT COLOR="$Color">$Text</FONT>");
}
printColored("This is black text");
print("<BR> ");
printColored("This is blue text", "blue");
print("<BR> ");
?>

Using unset with a Default Argument

Using unset with a Default Argument

You might wonder why you'd ever want to pull arguments out using the functions mentioned above instead of naming them in the declaration. It's possible that you do not know how many arguments you will be given. Consider a function that creates a list, given any number of items. You could first place those items in an array, then pass the array to the function, which in turn would pull the items out of the array. Alternatively, you could write a function that accepted a variable number of arguments, as I have in

Function with Variable Number of Arguments

<?
function makeList()
{
print("<OL> ");
for($i=0; $i <func_num_args(); $i++)
{
print("<LI>" . func_get_arg($i) . " ");
}
print("</OL> ");
}
makeList("PHP", "MySQL", "Apache");
?>

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics