Re: function array problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Feb 17, 2009 at 3:29 PM, Ashley Sheridan
<ash@xxxxxxxxxxxxxxxxxxxx> wrote:
> On Tue, 2009-02-17 at 15:13 -0500, Bastien Koert wrote:
>> On Tue, Feb 17, 2009 at 3:10 PM, Ashley Sheridan
>> <ash@xxxxxxxxxxxxxxxxxxxx>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
>> >
>> >
>> > --
>> > PHP General Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>>
>> i tend to do
>>
>> function addEvent($values='')
>> {
>>   if (!is_array($values))
>>      {
>>        $values = Array('name' => '', 'venue' => '', 'description' =>'',
>> 'errors' => Array());
>>      }
>>   //rest of the code
>>
>> }
>>
>>
>>
> It's a shame really, because to me it just seems darn messy to have to
> perform a check inside the function itself and initialise variables
> there. Putting such initialisations inside the parentheses seems more
> elegant. :(
>
>
> Ash
> www.ashleysheridan.co.uk

To each their own. I think having an array (especially nested arrays)
embedded in the parameter list of a function declaration like that
looks kind of ugly, but that's just me.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux