Passing data to function arguments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Passing data to function arguments

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No, not in PHP it can't :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved from Theory & Design to PHP Code ;)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
Post Reply