Re: Function definition: how to make default argument an empty array?
| From: | Bogdan Stancescu | Date: | Sat, 22 Dec 2001 12:16:13 +0000 |
| Subject: | Re: Function definition: how to make default argument an empty array? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-78560@lists.php.net to get a copy of this message | ||
Can't test it now, but have you tried
function makeyogurt ($flavour, $type = list())
I assume you need it to be an array because you want to either walk it or, more
likely, perform an in_array() test on it and you found that passing non-array
variables to in_array issues an error. If initializing as an array doesn't work
you can always do this:
function makeyogurt ($flavour, $type = "") {
while ((is_array($type)) && (list($key,$val)=each($type)))
{
[loop]
}
}
or this:
function makeyogurt ($flavour, $type = "") {
if ((is_array($type)) && (in_array("strawberry_yoghurt",$type)))
{
[loop]
}
}
or use the stone-age method:
function makeyogurt ($flavour, $type = "") {
if (@in_array("strawberry_yoghurt",$type))
{
[loop]
}
}
Bogdan
Michael Jurgens wrote:
> Hi,
>
> As you all may know, this is how you set an optional second argument, that
> defaults to acidophilus
>
> function makeyogurt ($flavour, $type = "acidophilus")
> { }
>
> I'm now looking for a way to have the second (optional) argument be an array
> of strings. I can't get it to work though...
>
> In pseudo-code:
>
> function makeyogurt ($flavour, $type = 'EMPTY ARRAY')
> { }
>
> Any help would be much appreciated,
> Michael
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net