Jump to content

Recommended Posts

All

 

My last question on sessions has got me thinking about case statements whick i believe are called switch statements in php

 

i have an array which contains a set of 1 s and 0 s as follows 0,1,0,0,1

 

What i want to do is run some code if its a 1 (true or false if you prefer)

 

 

I was kindly shown how to do this with an if statement as follows

 

session_start();
if(isset($_SESSION['permissions']))
$menus = array(
'menu1',
'<a href="http://www.google.co.uk">Menu2</a>'
,'menu3',
'menu4',
'menu5');

for($x = 0; $x < count($_SESSION['permissions']); $x++) {
  if($_SESSION['permissions'][$x]) {
// display the menu item
print $menus[$x]."<br />";

  }
}

 

Is there a way to do the same thing with a switch statement?

 

thanks

 

gibbo

 

Link to comment
https://forums.phpfreaks.com/topic/183589-switch-statement-and-arrays/
Share on other sites

Sure:

 

for($x = 0; $x < count($_SESSION['permissions']); $x++) {
  switch ($_SESSION['permissions'][$x]) {
      default: 
      case 1:// display the menu item
              print $menus[$x]."<br />";
       break;

       case 0:
                // other code here
  }
}

 

I believe that is what you were getting after?

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.