default argument values

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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"

Re: default argument values

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nikola Skoric wrote:

No.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Give a man a fish, he owes you one fish.
Teach a man to fish, you give up your monopoly on fisheries.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEGeN73jcQ2mg3Pc8RAuzIAJ9405fAxCU7dMY8Gqi5gFiTepDoLwCfZNqh
hP+JeBrEfRAudgd90HTy+YI=
=CgWw
-----END PGP SIGNATURE-----

Re: default argument values

only
function foo(\$b=2,\$a=1, \$c=3) {
}

foo(2);

Re: default argument values

Nikola Skoric wrote:

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);