Page 1 of 1

Passing data to function arguments

Posted: Mon Jan 23, 2006 11:47 am
by pilau
Yo. I have a question about function arguments.
Let's say I have the following function:

Code: Select all

<?php
function foo($bar, $baz = "baz", $big = '', $bool = false)
{

//Some code is executed here.
//Note that ALL of the function arguments are used!

}
?>
Now, as you can see the argments $baz and $big have default values.
Let's I'd like to call the function, only altering the value of the arguments $bar and $bool:

Code: Select all

foo("bar", "baz", '', true);
Could that be done without passing information to the 2nd and 3rd arguments?

Posted: Mon Jan 23, 2006 11:56 am
by Chris Corbyn
No, not in PHP it can't :)

Posted: Mon Jan 23, 2006 11:57 am
by Chris Corbyn
Moved from Theory & Design to PHP Code ;)

Posted: Mon Jan 23, 2006 12:27 pm
by pilau
Darn, so I can't skip function arguments in any way? <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>.
Now I have to memorize all of the most-commonly used style generating functions of vBulletin admin CP :P

Posted: Mon Jan 23, 2006 12:58 pm
by Chris Corbyn
You will indeed. When writing functions it's clearly sensible to think carefully about the order your parameters come in. If there's a super long list of arguments you might consider using an array as an argument. Since this isn't your own code it's not relevant though ;)

Posted: Mon Jan 23, 2006 1:15 pm
by pilau
Heh, correct, it isn't relevant.
Although they did orginize the arguments from the top most important and most used to the lesser ones.