note 40942 added to language.variables.variable

From: Date: Wed, 24 Mar 2004 02:50:18 +0000
Subject: note 40942 added to language.variables.variable
Groups: php.notes 
Request: Send a blank email to php-notes+get-67084@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.variables.variable.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40942 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40942&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40942&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67084) next »