Re: Feature?

From: Date: Wed, 26 Sep 2001 11:42:31 +0000
Subject: Re: Feature?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-68839@lists.php.net to get a copy of this message
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. ><?php >function foo($p) >{ > if (empty($p['fred'])) > $p['fred'] = 'hello'; // same for banana > echo $p['fred'] . $p['banana']; >} >foo(array('fred' => 'hello', 'banana' => >'world')); >?> >steve 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. <?php 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: "; foo(array()); ?>

« previous php.general (#68839) next »