Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

switch vs if else

Featured Replies

In my site at the moment for things like menus and all other things, I was thinking it might be easier to read code wise and also quicker if I change my if else statements to switches, for instance as an example in my menu code I have a block of html then some php to say if the user is moderator or admin add this, then back to normal html code, then more php to do the same check to add something else and it goes on for about 2 or 3 more things, would it be simpler and easier to just use a switch and write the whole block of code out for each user level?

 

I did it the other way so that when it comes to editing I don't necessarily need to change 3 lots of the same code it just changes the 1 piece, but it defiantly looks a bit messy!

It's up to you, either works. Which looks neater to you?

 

<?
switch ($user_level){
 case 3:
   include('admin.inc.php');
   break;
 case 2:
   include('moderator.inc.php');
   break;
 case 1:
   include('member.inc.php');
   break;
 default:
   include('guest.inc.php');
   break;
}

 

<?
if (3 == $user_level){
  include('admin.inc.php');
} elseif (2 == $user_level){
  include('moderator.inc.php');
} elseif (1 == $user_level){
  include('member.inc.php');
} else {
  include('guest.inc.php');
}

 

Not 100% sure why I naturally went decremental with this. Tiredness?

  • Author

Let me try and explain it a bit better.

 

I have coded my navigation bar for a user that is logged out, it has lots of little bits of code within it such as anchors, forms, <p>'s etc etc you get the idea. Then in the middle of the code at various places I have added 'if' statements to change a bit of the code depending on whether the viewer is a logged in user, a user that is a mod or a user that is admin, and as neat as I try to keep my code it is beginning to look messy because of all the different places where an 'if' statement is needed.

 

So I was wondering whether to have a switch statement and write the navigation bar out multiple times with the differences in user classifications;

 

switch ($user)
{
case 1:
 // Not a logged in user, display nav bar for non user
 break;
case 2:
 // Standard user, display nav bar for normal user
 break;
case 3:
 // Mod, display nav bar for moderator
 break;
case 4:
 // Admin, display nav bar for admin
 break;
}

 

or keep doing how I have done and write the nav bar code once and just change the parts that need changing as they need changing using if else

mmmm id say use a switch and add each link as needed

 

<?php
//Nav bar goes here
//Use a switch to add extra links that each cat of user may need
switch ($user)
{
case 1:
 //Login/register link maybe
 break;
case 2:
 //Logout link
 break;
case 3:
 // Logout link/Mod CP link
 break;
case 4:
 // Logout link/Admin CP link
 break;
}
?>

Edited by webdesigner93

  • Author

mmmm id say use a switch and add each link as needed

 

<?php
//Nav bar goes here
//Use a switch to add extra links that each cat of user may need
switch ($user)
{
case 1:
 //Login/register link maybe
 break;
case 2:
 //Logout link
 break;
case 3:
 // Logout link/Mod CP link
 break;
case 4:
 // Logout link/Admin CP link
 break;
}
?>

but the links don't just go at the end of the rest of the code, it is embedded amongst it which is where it is starting to look messy, there would need to be 4 switch statements if i did it that way. I think I'll leave it how it is as it is working fine. thanks for your help though

but the links don't just go at the end of the rest of the code, it is embedded amongst it which is where it is starting to look messy, there would need to be 4 switch statements if i did it that way. I think I'll leave it how it is as it is working fine. thanks for your help though

well u can also put the switch in the middle of the nav or where ever you need it , but whatever suits your needs will be fine

  • Author

So you have switch statement directly in your frontend view logic?

 

If so, ouch.

Why ouch? And no I'm using if else at the minute, I don't quite understand what you mean saying I have it directly in my front end view logic...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.