Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233991
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233995
Share on other sites

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().

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233996
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1233997
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1234001
Share on other sites

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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/240231-switch-vs-ifelse/#findComment-1234004
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.