Possible Duplicate:
named PHP optional arguments?
I want to do this:
function they_said($alice = "", $bob = "", $charlie = "") {
echo "Alice said '$alice'";
echo "Bob said '$bob'";
echo "Charlie said '$charlie'";
}
they_said($charlie => "Where are the cookies!?");
That way I can ignore passing the first 2 arguments and simply pass the one I want.
Here's an example in python.
One of the most important concepts in python is that of keyword arguments used in functions. Let us have a look at it in detail. What are keyword arguments in Python? In python, arguments are used in functions to pass on specific information when they are called so that their functionality can be tailored according to requirements.
A function in PHP can be defined to accept input from calling environment/script in the form of arguments. These arguments are given as comma separeted list inside the parentheses in front of name of function. Note that while calling a function, same number of arguments must be passed to it.
Python allows functions to capture any keyword arguments provided to them using the ** operator when defining the function: That ** operator will allow our format_attributes function to accept any number of keyword arguments. The given arguments will be stored in a dictionary called attributes.
In fact, some functions in Python force arguments to be named even when they could have been unambiguously specified positionally. Python 3’s sorted function requires that all arguments after the provided iterable ones to be specified as keyword arguments:
No, but you can pass an array:
function they_said($persons)
{
foreach ($persons as $person => $text)
{
echo "$person said '$text'";
}
}
they_said( array('charlie' => 'Where are the cookies!?') );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With