Welcome Guest, Not a member yet? Register   Sign In
How to set default argument of type array?
#1

[eluser]jbibbings[/eluser]
I have this code in my controller:

Code:
function edit_address($data = array()){

// Do boring stuff

$data['result'] = 'My Result';
$this->load->view('includes/address', $data);


}

I am trying to set an empty array as my default for the argument in edit_address. This is because edit_address may or may not be receiving arguments depending on where it gets called.

PHP keeps spitting out errors in my view though when I assign a default value to an array:

Code:
Message: Undefined variable: result


Any idea why this could be? As soon as I remove the default assignment everything works fine.
#2

[eluser]TWP Marketing[/eluser]
[quote author="jbibbings" date="1309216482"]I have this code in my controller:

Code:
function edit_address($data = array()){

// Do boring stuff

$data['result'] = 'My Result';
$this->load->view('includes/address', $data);


}

I am trying to set an empty array as my default for the argument in edit_address. This is because edit_address may or may not be receiving arguments depending on where it gets called.

PHP keeps spitting out errors in my view though when I assign a default value to an array:

Code:
Message: Undefined variable: result


Any idea why this could be? As soon as I remove the default assignment everything works fine.[/quote]

Try moving the array declaration out of the function parameter list:
Instead, pass the value of $result as the default value. You may override the default (an empty string) when you call the function: edit_address('some other result');
Code:
function edit_address($result = ''){

// Do boring stuff

$data = array(); // declare the array
$data['result'] = $result; // this will contain either the default value or the value passed

$this->load->view('includes/address', $data);

}




Theme © iAndrew 2016 - Forum software by © MyBB