RE: [PHP] Better way (if...elseif...else)

From: Date: Thu, 29 Mar 2001 22:00:32 +0000
Subject: RE: [PHP] Better way (if...elseif...else)
Groups: php.general 
Request: Send a blank email to php-general+get-46168@lists.php.net to get a copy of this message
One thing I would do different is: switch ($fcol){ case 000: break; case 050: break; default: break; } To me the switch/case code is easier to read/maintain. I know that doesn't actually solve your repetition problem but it will make it a little easier to look at. John Guynn This email brought to you by RFCs 821 and 1225. -----Original Message----- From: Ashley M. Kirchner [mailto:ashley@pcraft.com] Is there a better way to write the following snippet: if ($priority == "000") { $fcol="high"; $pstr .= "<option value=\"000\" selected>High\n"; $pstr .= "<option value=\"050\">Medium\n"; $pstr .= "<option value=\"100\">Low\n"; } elseif ($priority == "050") { $fcol="med"; $pstr .= "<option value=\"000\">High\n"; $pstr .= "<option value=\"050\" selected>Medium\n"; $pstr .= "<option value=\"100\">Low\n"; } else { $fcol="low"; $pstr .= "<option value=\"000\">High\n"; $pstr .= "<option value=\"050\">Medium\n"; $pstr .= "<option value=\"100\" selected>Low\n"; } I just hate having to repeat pieces of code. This piece here just generates a drop down list of items, with the current one being the selected one.

« previous php.general (#46168) next »