Reputation: 2952
If I had a PHP function, that had a load of default arguments, all of which were set to false
, for example:
function foo($foo = false, $bar = false, $foobar = false)
{
}
Is there a quicker (not in execution, but coding style, practice, number of characters it takes to write the code etc.) way of doing this?
Upvotes: 0
Views: 60
Reputation: 10880
function foo($f = 0 , $b = 0 , $fb = 0)
you can use 0 instead of false.
Upvotes: 0
Reputation: 7585
function foo(){
$defaults = array_merge(array_fill(0, 3, false), func_get_args());
}
foo(); //defaults is array(false, false, false);
i prefer passing arrays to functions/methods over the way you are doing
function foo($defaults = array()){
$defaults = array_merge(array('foo' => false, 'bar' => false, 'etc' => false), (array)$defaults);
...
echo $defaults['foo']; // always set
...
}
Upvotes: -1