Does PHP have "named parameters" so that early arguments can be omitted and later arguments can be written?

default-arguments, named-parameters, php, php-8

Solution

Use arrays :

function test($options = array()) {
    $defaults = array(
        't1' => 'test1',
        't2' => 'test2',
        't3' => 'test3',
    );
    $options = array_merge($defauts, $options);
    extract($options);
    echo "$t1, $t2, $t3";
}

Call your function this way :

test(array('t3' => 'hi, i am different'));

Problem

In PHP you can call a function with no arguments passed in so long as the arguments have default values like this: ``` function test($t1 ='test1',$t2 ='test2',$t3 ='test3') { echo "$t1, $t2, $t3"; } test(); ``` However, let's just say I want the last one to be different but the first two parameters should use their default values. The only way I can think of is by doing this with no success: ``` test('test1','test2','hi i am different'); ``` I tried this: ``` test(,,'hi i am different'); test(default,default,'hi i am different'); ``` Is there clean, valid way to do this?

Original source

Related problems