Is it possible to skip parameters that have default values in a php(5) function call?

后端 未结 5 1703
长情又很酷
长情又很酷 2020-12-02 00:22

I have this:

function foo($a=\'apple\', $b=\'brown\', $c=\'Capulet\') {
    // do something
}

Is something like this possible:



        
5条回答
  •  自闭症患者
    2020-12-02 01:05

    If it's your own function instead of one of PHP's core, you could do:

    function foo($arguments = []) {
      $defaults = [
        'an_argument' => 'a value',
        'another_argument' => 'another value',
        'third_argument' => 'yet another value!',
      ];
    
      $arguments = array_merge($defaults, $arguments);
    
      // now, do stuff!
    }
    
    foo(['another_argument' => 'not the default value!']);
    

提交回复
热议问题