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
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