...

View Full Version : Strange Swtich Behaviour



Y-STU-K
07-04-2005, 02:59 PM
First of all I always thought that if switch did not find a match for the variable you gave it, it would drop through to the default clause. Apparently not, In my scripts it seems to completely ignore it. If I have a case with no break i'd expect it drop to the default clause but it just executes the next one down :eek: i'm sure thats incorrect behaviour.



switch($action)
{
case 'action1' :
//do something here but I don't need a break

case 'action2' :
//do something but I do need a break after this one.
break;

default:
//my default action
break;


If does action1 as I wished it to and it does it and then does action2 and exits due to the break on action2.

My Second problem I'm having is trying to use a condition on a case I decided that this was a good way to try and test for correct permissions.



switch($action)
{
case 'action1' && $_SESSION['permissions']->isSuperUser() :
//do something here but only if your a super user
break;
case 'action2' && $_SESSION['permissions']->isSuperUser() :
//do something but I do need a break after this one.
break;

default:
//my default action
break;


That I see as being a nice way to test and it should work that way shouldn't it well according to the PHP manual is does. The object $_SESSION['permissions'] returns a boolean true if the user logged in is a super user. I keep a permissions object stored in a sessions for convience. Again if the user is not a super user they can't run the action and it should drop to the default action. In fact when the above code is run it just seems to ignore the condition and runs the first action anyway.

This is currenlty making me quite anoyed as a case should be able to handle it and I don't want to have to use if elseif and else as that would be slower in this case does anyone know what i'm doing wrong or am I expecting too much of the switch statement

schleppel
07-04-2005, 03:39 PM
For the second one i'd do this:


switch(TRUE)
{
case ($action == 'action1' && $_SESSION['permissions']->isSuperUser()):
//do something here but only if your a super user
break;
case ($action == 'action2' && $_SESSION['permissions']->isSuperUser()):
//do something but I do need a break after this one.
break;
default:
//my default action
break;
}


EDIT: I tried you first code (slightly modified):


<?php
switch($action)
{
case 'action1' :
echo '1';
case 'action2' :
echo '2';
break;
default:
echo 'default';
break;
}
?>

// Results
// $action = 'action1' echoed 12
// $action = 'action2' echoed 2
// $action = 'action3' echoed default


is that not the expected result? When it finds no break it's supposed to keep going until it finds a break.

Y-STU-K
07-04-2005, 03:51 PM
for the first example I did I expected that if i entered 'action1' and with no break it drops down to default and doesn't just execute the other actions until it finds a break; if thats how its meant to work that is truely irritating behaviour.

Ok got it working now :) my permissions are now properly checked for each action thankyou for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum