|  | Posted by ZeldorBlat on 06/16/60 11:42 
Nikola Skoric wrote:> Hi there, is something like this possible in PHP:
 >
 > function foo($a=1, $b=2, $c=3) {
 >  //...
 > }
 >
 > foo($b=2);
 >
 > Will this assign $a and $c to default values?
 >
 > --
 > "Now the storm has passed over me
 > I'm left to drift on a dead calm sea
 > And watch her forever through the cracks in the beams
 > Nailed across the doorways of the bedrooms of my dreams"
 
 Unfortunately it doesn't work that way.  The default is only taken when
 the argument is not specified at all and I don't think you can name the
 parameters like that when you call the function.  The best you can do
 is something like this:
 
 function foo($a = 1, $b = 2, $c = 3) {
 //do something
 }
 
 //Is valid, $a = 5, $b = 2, $c = 3
 foo(5);
 
 //Is valid, $a = 5, $b = 42, $c = 3
 foo(5, 42);
 
 //Syntactically valid, but results in $a = 5, $b  = 2, and $c = 3
 inside the function.
 //Basically assign 5 to $b (outside the function) then pass the result
 (5) as the first
 //parameter to foo():
 foo($b = 5);
  Navigation: [Reply to this message] |