0

I have this problem but I don't know how to name it, for instance, I have this function,

function test($param1,$param2 = false,$param3 = false)
{

    if($param2 == true)
    {
        return '2';
    }

    elseif($param3 == true)
    {
        return '3';
    }

    else 
    {
        return '1';
    }
}

In order to return '3', I have to evoke my function like this,

$test = test('test',false,true);

I want to ask whether I can skip the second param and return '3' by evoking the function like this?

$test = test('test',$param3 = true);

Or any other method to do so?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Run
  • 54,938
  • 169
  • 450
  • 748
  • With PHP 8.x you can use [named parameters](https://wiki.php.net/rfc/named_params) to skip arguments. – Picard Sep 07 '22 at 11:35

5 Answers5

4

You can fake named parameters, as seen here on SO:

Emulating named function parameters in PHP, good or bad idea?

Here is a sample of the correct answer you should be looking for on this page:

function myFunc (array $args) {
    $default = array(
        "name" => "John Doe",
        "age" => "30"
    );
    // overwrite all the defaults with the arguments
    $args = array_merge($defaults, $args);
    // you *could* extract($args) here if you want

    echo "Name: " . $args['name'] . ", Age: " . $args['age'];
}

myFunc(array("age" => 25)); // "Name: John Doe, Age: 25"
Community
  • 1
  • 1
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • thanks. I think it should not be a 's' at `$defaults`. The line should be - `$args = array_merge($default, $args);` – Run Mar 22 '11 at 16:57
  • With PHP 8.x you can use [named parameters](https://wiki.php.net/rfc/named_params) to skip arguments. – Picard Sep 07 '22 at 11:34
1

You could pass the parameters in an array:

$params = array(
    'param1' => 'test',
    'param3' => true
);

$result = myFunction($params);

function myFunction($params) {
    if (isset($params['param3']) && $params['param3']) {
        return 3;
    }
}
Toto
  • 89,455
  • 62
  • 89
  • 125
0

You can set default values for your paramaters like this:

function someFunction( $varA, $varB=false, $varC=false)
{
    if($varB==true)
    {
      return 'b';
    }
}

When you call the function, you can do someFunction(true), someFunction(true, true), or someFunction(true, true, true). The only catch is you can't declare A and C without also declaring B.

Edit: Missed first line. You could also pass your variables as an array.

Ryre
  • 6,135
  • 5
  • 30
  • 47
0

No, there is no way to do that. If you want to pass $param3 to the function, you will have to pass all the parameters preceding $param3 in the function signature.

Imi Borbas
  • 3,683
  • 1
  • 19
  • 16
0

I want to ask whether I can skip the second param and return '3' by evoking the function like this?

No, you cannot. The only way to "skip" a parameter is by sending something like NULL. But then your function needs to look like:

function foo($a, $skip_me = NULL, $b = 1);
{
  if ($skip_me === NULL) $skip_me = false; // set the default value like this
}

foo(1, NULL, 2);

The other option is to send an array and have the function take a single parameter. That can be useful if you have a lot of optional arguments.

Matthew
  • 47,584
  • 11
  • 86
  • 98