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.
foo("blah", , "test");
? – SteelgetArrayValue($array, $key, $default)
- if $key doesn't exist in $array, $default is returned, or I usefunction doSomething($a, $b = null, $c = null)
and useif(is_null($b)) $b = $bDefault)
within the function body. This way I can calldoSomething("bob", null, "fred")
and the null will be overridden with the default value of $b. – Deadening