Sruj
Sruj

Reputation: 1207

How to set default value for assocc array function argument in PHP?

How to correctly write default value for assocc array function argument?

function foo($arr['key']='value');

Upvotes: 2

Views: 94

Answers (1)

Marvin Fischer
Marvin Fischer

Reputation: 2572

<?php

function foo($arr = null)
{
    if (is_null($arr))
    {
        $arr = array(
            'key'   =>  'value'
        );
    }

    ...

You cant use the direct way you tried above. Just work with this little workaround

Else you might go with this:

function foo($a = array('key' => 'value'))
{
    ...

But in my opinion its a bit unhandy to declare an array in the function head. Its purely on you how you want to use it

Upvotes: 3

Related Questions