can php function parameters be passed in different order as defined in declaration

Absolutely not.

One way you'd be able to pass in unordered arguments is to take in an associative array:

function F($params) {
   if(!is_array($params)) return false;
   //do something with $params['A'], etc...
}

You could then invoke it like this:

F(array('C' => 3, 'A' => 1, 'B' => 1));

No, but you could pass in an associative array to handle this:

f(array('C'=>3, 'A'=>1, 'B'=>1));

and then access it by:

function f($obj)
{
   $c = $obj['C'];
}

PHP doesn't have named arguments, so the short answer is no. There are two solutions I can find, although neither are all that fantastic.

You could define your function to take its arguments as an array, like this:

function image($img) {
    $tag  = '<img src="' . $img['src'] . '" ';
    $tag .= 'alt="' . ($img['alt'] ? $img['alt'] : '') .'">';
    return $tag;
}

$image = image(array('src' => 'cow.png', 'alt' => 'cows say moo'));
$image = image(array('src' => 'pig.jpeg'));

That method unfortunately requires that you modify your function, and as a result I don't like it. The other option is to use this wrapper class which lets you use named arguments in one of the following ways:

$obj->method(array('key' => 'value', 'key2' => 'value2'));
$obj->method(':key = value', ':key2 = value2');

Tags:

Php