You are here: Re: default argument values « PHP Programming Language « IT news, forums, messages
Re: default argument values

Posted by ZeldorBlat on 11/18/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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация