Jonathon Oates
Jonathon Oates

Reputation: 2952

Quicker way to specify default function arguments in php

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

Answers (3)

Sabeen Malik
Sabeen Malik

Reputation: 10880

function foo($f = 0 , $b = 0 , $fb = 0) 

you can use 0 instead of false.

Upvotes: 0

dogmatic69
dogmatic69

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

mvds
mvds

Reputation: 47074

function foo($foo=!1,$bar=!1,$foobar=!1)
{
}

Upvotes: 2

Related Questions