RE: [PHP] Looking for optimal coding
| From: | Ford, Mike [LSS] | Date: | Fri, 15 Feb 2002 13:36:45 +0000 |
| Subject: | RE: [PHP] Looking for optimal coding | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-84895@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Joffrey van Wageningen [mailto:joffrey@playcollective.com]
> Sent: 15 February 2002 09:00
>
> i would try:
>
> for($i=01;$i<=50;$i++) {
> if(!empty($content) && $row[$content] == $states[$i])
> $selected = " selected";
> elseif($dstate == $states[$i])
> $selected = " selected";
> else
> $selected = "";
> echo "<option
>
> value=\"".$states[$i]."\"".$selected.">".$nstates[$i]."</option>";
> }
Well, all the boolean operators use lazy evaluation, so you can safely collapse your tests into a
single one. Also, using the ?: operator, you can turn the whole thing into a single echo statement:
for($i=01;$i<=50;$i++):
echo "<option value=\"${states[$i]}\"
.((!empty($content) && $row[$content] == $states[$i]) || $dstate == $states[$i]
? " selected" : "")
.">${nstates[$i]}</option>";
endfor;
Mind you, I wouldn't particularly claim this to be particularly readable, but it's about
as compact as you can get!
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211