Using Default Arguments in a Function

I am confused about default values for PHP functions. Say I have a function like this:

function foo($blah, $x = "some value", $y = "some other value") {
    // code here!
}

What if I want to use the default argument for $x and set a different argument for $y?

I have been experimenting with different ways and I am just getting more confused. For example, I tried these two:

foo("blah", null, "test");
foo("blah", "", "test");

But both of those do not result in a proper default argument for $x. I have also tried to set it by variable name.

foo("blah", $x, $y = "test");   

I fully expected something like this to work. But it doesn’t work as I expected at all. It seems like no matter what I do, I am going to have to end up typing in the default arguments anyway, every time I invoke the function. And I must be missing something obvious.

16 Answers
16

Leave a Comment