In the following snippet, how does printPhrase
know if the passed arguments are $a
and $b
(so it uses default value of $c
, or $a
and $c
(so it uses default value of $b
)?
private function printPhrase ($a, $b='black', $c='candle!' ) {
echo $a . $b . $c; //Prints A black cat! or A black candle!
}
private function callprintPhrase () {
printPhrase('A ', ' cat!');
}
In php arguments always passes from left to right with out skip. So
printPhrase('A ', ' cat!');
always fills with values first and second argument of function.http://php.net/manual/en/functions.arguments.php#functions.arguments.default
There is exists proposal to skip params.
If you want to use default params you need to rewrite your code like in this answer: https://stackoverflow.com/a/9541822/1503018