allex01 Posted June 29, 2008 Share Posted June 29, 2008 Is there a way to minimize this code. elseif($page=="index1.php"){$current3="current";} elseif($page=="index2.php"){$current3="current";} elseif($page=="index3.php"){$current3="current";} elseif($page=="index4.php"){$current3="current";} elseif($page=="index5.php"){$current3="current";} elseif($page=="index6.php"){$current3="current";} Basicly i want $current3= current if any of the pages occur in $page. I tried using or operator as you see below, but that didn't work elseif($page==("index1.php"||"index2.php"||"index3.php"||"index4.php")){$current3="current";} Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/ Share on other sites More sharing options...
hitman6003 Posted June 29, 2008 Share Posted June 29, 2008 Use a switch statement... http://www.php.net/switch Quote Link to comment https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/#findComment-577039 Share on other sites More sharing options...
.josh Posted June 29, 2008 Share Posted June 29, 2008 well I don't know what the bigger picture here is, but based off just that code, suppose you could do: if((substr($page,5,1) >= 1) && (substr($page,5,1) <= 6)) { $current3="current"; } edited to fix 2nd substr argument (forgot it started at 0) Quote Link to comment https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/#findComment-577044 Share on other sites More sharing options...
allex01 Posted June 29, 2008 Author Share Posted June 29, 2008 Thanks for the responses. What i'm trying to do is set current selection for my css menu so if page is page1.php or page2.php or page3.php for exmaple i want the current menu selection to at home which has a css class definition of $current1 see below <li class="<?php echo $current1;?>"><a. href.......>Home</a></li> <li class="<?php echo $current2?>"><a. href.......>Audio</a></li> .. The solution you provided below assuming the name formats are index1 index2 .... Not all pages have the same character count or same name format. We might have a page with the name signup_here.php. So it will not work. f((substr($page,5,1) >= 1) && (substr($page,5,1) <= 6)) { $current3="current"; } I want to learn of a nice way to optimize the code. I appreciate all your help. Quote Link to comment https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/#findComment-577066 Share on other sites More sharing options...
PFMaBiSmAd Posted June 29, 2008 Share Posted June 29, 2008 To test if a variable matches a list of different values, store the values in an array and use the php in_array() function. Quote Link to comment https://forums.phpfreaks.com/topic/112397-better-way-to-write-this-code/#findComment-577072 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.