HDFilmMaker2112 Posted June 23, 2011 Share Posted June 23, 2011 Looking to know if I should convert the below to a switch statements for efficiency? All the opening and closing php tags is getting to me. <div class="tab"> <? if(isset($_SESSION['myusername'])){?> <a href="<? echo $page; ?>.php?films">Films</a><? } elseif(isset($_SESSION['myusername2'])){?> <a href="<? echo $page; ?>.php?contact">Contact</a><? } else { ?> <a href="<? echo $page; ?>.php?donate">Donate</a> <? } ?> </div> <div class="tab"> <? if(isset($_SESSION['myusername'])){?> <a href="index.php?webseries">Webseries</a><? } elseif(isset($_SESSION['myusername2'])){?> <a href="index.php?faq">FAQ</a><? } else { ?> <a href="<? echo $page; ?>.php?contact">Contact</a> <? } ?> </div> <div class="tab"> <? if(isset($_SESSION['myusername'])){?> <a href="index.php?company">Company</a><? } elseif(isset($_SESSION['myusername2'])){?> <a href="http://store.makethemoviehappen.com">Store</a><? } else { ?> <a href="<? echo $page; ?>.php?faq">FAQ</a> <? } ?> </div> <div class="tab"> <? if(isset($_SESSION['myusername'])){?> <a href="index.php?contact">Contact</a><? } elseif(isset($_SESSION['myusername2'])){?> <a href="index.php?usercp">User CP</a><? } else { ?> <a href="<? echo $page; ?>.php?investors">Investors</a> <? } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/ Share on other sites More sharing options...
AbraCadaver Posted June 23, 2011 Share Posted June 23, 2011 You could, but the main problem is that you are breaking in and out of PHP when you don't need to: if(isset($_SESSION['myusername'])){echo '<a href="'.$page.'.php?films">Films</a>'; } elseif(isset($_SESSION['myusername2'])){echo '<a href="'.$page.'.php?contact">Contact</a>'; } else { echo '<a href="'.$page.'.php?donate">Donate</a>'; } But if you do a lot of PHP in HTML instead of HTML echoed by PHP then you might also like: http://php.net/manual/en/control-structures.alternative-syntax.php Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233991 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 23, 2011 Author Share Posted June 23, 2011 It says right on php.net it's always better to break out of php to display HTML code. I always thought breaking in and out like that would slow things down but apparently it's what you should be doing. Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233993 Share on other sites More sharing options...
AbraCadaver Posted June 23, 2011 Share Posted June 23, 2011 It says right on php.net it's always better to break out of php to display HTML code. I always thought breaking in and out like that would slow things down but apparently it's what you should be doing. I've never seen that and it definitely is not ALWAYS better. Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233995 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 23, 2011 Author Share Posted June 23, 2011 Finally find the link to that page: http://us3.php.net/manual/en/language.basic-syntax.phpmode.php The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print(). Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233996 Share on other sites More sharing options...
AbraCadaver Posted June 23, 2011 Share Posted June 23, 2011 Finally find the link to that page: http://us3.php.net/manual/en/language.basic-syntax.phpmode.php The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print(). "large blocks of text" Your text in the first if was only 9 characters! Not large. Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233997 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 23, 2011 Author Share Posted June 23, 2011 Here's what I ended up with... cut down on the redundant text as much as possible. <div class="tab"> <a href="<?php echo $page; if(isset($_SESSION['myusername'])){ '.php?films">Films';} elseif(isset($_SESSION['myusername2'])){ echo '.php?contact">Contact';} else { echo '.php?donate">Donate';} ?> </a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1234001 Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2011 Share Posted June 23, 2011 For something like a menu, where you have multiple variable-size lists of choices, you would typically store the choices somewhere (database, array) and write some general-purpose code to take the correct choices and output the information the way you want (something like using a query to get a specific set of data that you need.) <?php // define menus $menu = array(); $menu['user_typeA'] = array("<a href='$page.php?films'>Films</a>","<a href='index.php?webseries'>Webseries</a>","<a href='index.php?company'>Company</a>"); $menu['user_typeB'] = array("<a href='$page.php?contact'>Contact</a>","<a href='index.php?faq'>FAQ</a>","<a href='http://store.makethemoviehappen.com'>Store</a>"); $menu['user_typeC'] = array("<a href='$page.php?donate'>Donate</a>","<a href='$page.php?contact'>Contact</a>","<a href='$page.php?faq'>FAQ</a>"); // logic to determine which menu to use if(isset($_SESSION['myusername'])){ $type = 'user_typeA'; } elseif(isset($_SESSION['myusername2'])){ $type = 'user_typeB'; } else { $type = 'user_typeC'; } // produce and output the correct menu foreach($menu[$type] as $item){ echo "<div class='tab'>"; echo $item; echo "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1234004 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.