Re: function array problem
| From: | Andrew Ballard | Date: | Tue, 17 Feb 2009 20:19:47 +0000 |
| Subject: | Re: function array problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-288483@lists.php.net to get a copy of this message | ||
On Tue, Feb 17, 2009 at 3:10 PM, Ashley Sheridan
<ash@ashleysheridan.co.uk> wrote:
> I've had a bit of a problem with a function I'm using for a form.
> Essentially, the function looks like this:
>
> function addEvent($values = Array('name' => '', 'venue' =>
> '',
> 'description' => '', 'errors' => Array()))
> {
> // code here displays the form
> }
>
> The function is used to both display an empty form, and the form
> populated with values again should there be any validation errors.
>
> Now this works fine when the form has been filled out and there are
> errors present, as I can call the function with the correct array
> values. However, when I call the function with no arguments (intending
> the function to populate the $values array itself) all it does is
> present me with an empty array. A print_r($values) just returns
> Array( ), no key values defined.
>
> I altered the function to this:
>
> function addEvent($values = Array())
> {
> if(count($values) == 0)
> {
> $values = Array('name' => '', 'venue' =>
> '', 'description' =>
> '', 'errors' => Array());
> }
> // code here displays the form
> }
>
> then all works as intended. Question is, am I being dense, or is there a
> reason why this shouldn't work?
>
>
> Ash
> www.ashleysheridan.co.uk
>
>
It seems to work OK for me. I typically use your altered format or the
form that Bastien posted (except I usually default the parameters to
null rather than an empty string) though.
<?php
function addEvent($values = Array('name' => '', 'venue' =>
'',
'description' => '', 'errors' => Array()))
{
// code here displays the form
print_r($values);
}
addEvent();
?>
X-Powered-By: PHP/5.2.0
Content-type: text/html
Array
(
[name] =>
[venue] =>
[description] =>
[errors] => Array
(
)
)
Andrew