On Tue, 25 Sep 2001 09:36:57 -0500, you wrote:

>>> <?php
>>> function foo($p)     {      echo  $p['fred'] . $p['banana'];     }
>>> foo(array('fred' => 'hello', 'banana' => 'world'));
>>> ?>

>> Yeap, I know that. I thought about this bit of hack but this breaks the 
>> conception on giving parameters. Also this trick cannot
>> solve the problem with default parameters. If I have function with 5 
>> params and all of them have default values and I want to pass
>> value only to the second parameter what I have to do?

>Maybe this? A bit verbose, but functional.
>function foo($p)
>   if (empty($p['fred']))
>     $p['fred'] = 'hello';     // same for banana
>   echo  $p['fred'] . $p['banana'];
>foo(array('fred' => 'hello', 'banana' => 'world'));

I'm not keen on adding lots of multi-line if statements - I prefer tp
put things into arrays where I can.  Easier to update them.

function foo($p)
        $foodefault = array(    // prefix matching the name in 'extract'
                'foo_fred'=> '<strong>default fred</strong>',
                'foo_banana' => '<strong>default bananananana</strong>',
        extract ($p, EXTR_PREFIX_ALL, 'foo');   // get params
        extract ($foodefault, EXTR_SKIP, "foo");        // get defaults
        echo  "$foo_fred / $foo_banana";
echo "Both in place: ";
foo(array('fred' => 'hello', 'banana' => 'world'));
echo "<br /> Now with a missing param: ";
foo(array('fred' => 'hello'));
echo "<br /> Now both missing params: ";

PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to