note 40942 deleted from language.variables.variable by nlopess

From: Date: Wed, 24 Mar 2004 14:54:41 +0000
Subject: note 40942 deleted from language.variables.variable by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-67100@lists.php.net to get a copy of this message
Note Submitter: arjini@mac.com ---- If you are needing to set up variables in a function and accept parameters in a single variable, but have defaults, try the following: <? function someFunction($params){ $defaults = array( 'name1'=>'val1', 'name2'=>'val2', 'name3'=>'val3'); foreach($defaults as $var=>$default){ ${$key} = $default if(isset($params[$var])) ${$key} = $params[$key]; if(isset($_GET[$var])) ${$key} = $_GET[$key]; } /* depending on what was set, you should now how $name1 $name2 $name3 set to the appropriate value */ } ?> The $_GET part is just an example of how it could be extended. I found this whole construct most useful when programming custom smarty functions at first, but I've since used it for a lot of other things. It creates a nice namespace within your function, let's you set defaults and can easily be extended to automagically handle values from the superglobal arrays.

« previous php.notes (#67100) next »