Re: need help converting code to more efficient loop

From: Date: Sun, 10 Mar 2002 04:33:14 +0000
Subject: Re: need help converting code to more efficient loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-87896@lists.php.net to get a copy of this message
On Saturday 09 March 2002 18:08, Timothy J. Luoma wrote: > Hello! I am trying to reduce the size of the code below, which I believe > can be simplified. > > I checked the 'for' entry in the manual as well as googling for some > similar code, but did not have any luck. > > Here is what I have: > > > $ICON_COUNT="0"; > > if ($SHOW_WAI_ICON != "no") > { > $ICON_COUNT++ ; > } > > if ($SHOW_BOBBY508_ICON != "no") > { > $ICON_COUNT++ ; > } > > if ($SHOW_W3_ICON != "no") > { > $ICON_COUNT++ ; > } > > if ($SHOW_CSS_ICON != "no") > { > $ICON_COUNT++ ; > } > > > Now I would like to make that a 'for' loop, but I'm not sure how to do it > in PHP. > > In case it is not clear what I am trying to do, this is what I would do if > PHP syntax were like bash: > > variables="SHOW_CSS_ICON SHOW_W3_ICON SHOW_BOBBY508_ICON SHOW_WAI_ICON" > > for i in $variables > do > > if [ "$i" != "no" ] > then > #increment icon_count here > fi > > done > > > I'm just not sure about the nested syntax in PHP. $doo = array("SHOW_CSS_ICON", "SHOW_W3_ICON"); foreach($doo as $dah) { if (${$dah} != "no") { $ICON_COUNT++; } } *** Untested, use with caution! *** -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk /* Don't put off for tomorrow what you can do today because if you enjoy it today, you can do it again tomorrow. */

« previous php.general (#87896) next »