Jump to content

switch statement and arrays


gibbo1715

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.