2

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

function foo($arr['key']='value');
1
  • 1
    '$arr = array('string' => 'string value');' Commented Jul 13, 2015 at 13:36

1 Answer 1

3
<?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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.