Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can php functions have keyword arguments? (like python) [duplicate]

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.

like image 971
Alexander Bird Avatar asked Oct 09 '11 14:10

Alexander Bird


People also ask

What are keyword arguments 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.

How to call a function in PHP with arguments?

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.

How do I get the arguments of a python function?

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.

Why do some Python functions force arguments to be named?

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:


1 Answers

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!?') );
like image 172
ComFreek Avatar answered Oct 10 '22 10:10

ComFreek