wfcentral Posted February 28, 2008 Share Posted February 28, 2008 there must be a shorter way of writing this... it is just a portion of the code I'm using and it's only going to get longer... if ($row['label']=='Family Attraction') {$color='ORANGE';} if ($row['label']=='Museum') {$color='ORANGE';} if ($row['label']=='Arts') {$color='ORANGE';} if ($row['label']=='Landmark') {$color='ORANGE';} if ($row['label']=='Italian') {$color='RED';} if ($row['label']=='Greek') {$color='RED';} if ($row['label']=='Steak') {$color='RED';} if ($row['label']=='Seafood') {$color='RED';} Quote Link to comment https://forums.phpfreaks.com/topic/93424-short-way-of-writing-if-or-or-or-or-statement/ Share on other sites More sharing options...
Psycho Posted February 28, 2008 Share Posted February 28, 2008 I see three options: You could combine the options: <?php if ($row['label']=='Family Attraction' || $row['label']=='Museum') || $row['label']=='Arts' || $row['label']=='Landmark') { $color='ORANGE'; } if ($row['label']=='Italian' || $row['label']=='Greek' || $row['label']=='Steak' || $row['label']=='Seafood') { $color='RED'; } ?> You could use a switch which is better programatically, but not shorter <?php switch ($row['label']) { case 'Family Attraction': case 'Museum': case 'Arts': case 'Landmark': $color = 'ORANGE'; break; case 'Italian': case 'Greek': case 'Steak': case 'Seafood': $color = 'RED'; break; } ?> Or you could use an array: <?php $colors = array ( 'Family Attraction' => 'ORANGE', 'Museum' => 'ORANGE', // etc.. } $color = $colors[$row['label']]; ?> Which one I would use would depend upon how often the list changes, how big the list is, etc. But, without more information I would suggest using the switch() statement. Quote Link to comment https://forums.phpfreaks.com/topic/93424-short-way-of-writing-if-or-or-or-or-statement/#findComment-478636 Share on other sites More sharing options...
wfcentral Posted February 28, 2008 Author Share Posted February 28, 2008 thanks that works great - I used the switch... Quote Link to comment https://forums.phpfreaks.com/topic/93424-short-way-of-writing-if-or-or-or-or-statement/#findComment-478655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.