Re: Undefined variables

From: Date: Fri, 31 May 2002 16:37:15 +0000
Subject: Re: Undefined variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-100120@lists.php.net to get a copy of this message
Another similar way, using an array (very simple to add tasks): $tasks = array('ShowVersion','GetData','CreateImage'); if (@in_array($_REQUEST['Task'], $tasks)) { // add some error checking here (function_exists()) $$Task(); } else { // maybe run a default task here print "unknown task"; } Am using @ in case 'Task' doesn't exist at all, so no error will be seen (undefined variables create E_NOTICE level errors). Although you may want to check this before Task even reaches in_array(), with empty(). See also: http://www.php.net/error_reporting $$Task() is a variable function: http://uk.php.net/manual/functions.variable-functions.php And $_REQUEST is a PHP predefined/reserved variable that contains a mix of Get, Post and Cookie data. This may or may not be appropriate here: http://au.phpz.net/manual/language.variables.predefined.php On a sidenote (picky), consider making variables all lowercase. Also when printing a single var, no need to surround it with quotes. print $foo. Regards, Philip Olson On Fri, 31 May 2002, Christopher J. Crane wrote: > I like this piece of code. In fact, I convert all my scripts that use the > older If/Else code. What would happen if the "break; " wasn't used. Would > it just continue through the rest of the function to find another match??? > > "Miguel Cruz" <mnc@stoic.net> wrote in message > news:Pine.LNX.4.44.0205302126560.28702-100000@stoic.net... > > On Thu, 30 May 2002, Crane, Christopher wrote: > > > if ($Task == "ShowVersion") { function ShowVersion(); } > > > elseif ($Task == "GetData") { function GetData(); print > > > "$DataOutput"; } > > > elseif ($Task == "CreateImage") { function CreateImage(); } > > > else { print "Incorrect Variable or no Variable Supplies<br>"; } > > > > if (isset($Task)) > > { > > switch($Task) > > { > > case 'ShowVersion': > > ShowVersion(); > > break; > > case 'GetData': > > GetData; > > print $DataOutput; > > break; > > case 'CreateImage': > > CreateImage(); > > break; > > default: > > print 'Unknown function'; > > } > > } else { > > print 'No function supplied'; > > } > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#100120) next »