Forum Moderators: coopster
Is there a way to have an optional argument keep there default value?
Example:
function test($required1, $required2, $optional1 = 'test1', $optional2 = 'test2') {
//do something
} test('Req1', 'Req2', , 'Test2New'); So that $optional1 will stay 'test1' and i just skip it.
The way it is show above doesn't work and gives an error about an argument is missing.
Is there a way to let it have its value without having to type it again?
Thx in advanced.
For as far as I know this can not be done, but a workaround I use in these (rare) cases is the following:
function test($required1, $required2, $optional1 = FALSE, $optional2 = 'test2')
{
if ($optional1 === FALSE)
{
$optional1 = 'test1';
}
//do something
}
Assign $optional1 a default value of a type which it will never receive when being called, type Boolean in this example.
The function should be called then:
test('Req1', 'Req2', FALSE , 'Test2New');
Of course now you don't really 'skip' the argument but when calling the function you don't have to bother about the default value to fill in.
The same can be done for $optional2, but as it is the last argument there is no need to do it.
Regards,
Arjan
if you most often leave out the one you presently have next to last then maybe you should make it the last argument, otherwise I would say you logic is flawed.
<added>that workaround makes no sense
you are still passing false and having it default to another value, the default false in the function definition is redundant. Removing the default value of false would produce the same result as if you have it there.
again, logic flaw
Example Function Declaration
demo(a, b = 2, c = 3, d = 4)
b,c,d will retain 2,3,4 respectively if you demo(a_value) but b will not have a value if you demo(a_value,,c_value,d_value)
The best way to work around this, is to rewrite the function in the manner of whatever will be left out would be the last value.
Or outside of the call, set up a comparison to just use the default values.
i.e.
$req1, $req2, $opt1, $opt2 are the variables being sent to test()
$opt1 = (empty($opt1))? 'default_value' : $opt1;
with that code, you can ensure that opt1 either has a value, or the default value.
you are still passing false and having it default to another value, the default false in the function definition is redundant. Removing the default value of false would produce the same result as if you have it there.
Also the option given by Mr_Fern won't work in that particular situation (although it looks a lot like my solution), you still have to fill in a value for argument 3, as you may not skip it, when the function is called with argument 4 set to a non-default value. In that situation empty($opt1) is false then and the passed value will be set in $opt1 and not the default value. So in that case you still have to change 812 function calls when the default value for argument 3 is changed.
By the way, I agree that when argument 3 has its default most of the calls and argument 4 not, change their order. Or make different functions for all situations and let those functions fill in the default values before they call test(...).
Regards,
Arjan