Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    UK
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Strange Swtich Behaviour

    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 i'm sure thats incorrect behaviour.

    PHP Code:
    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.

    PHP Code:
    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

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    For the second one i'd do this:
    PHP Code:
    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 Code:
    <?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.
    Last edited by schleppel; 07-04-2005 at 03:47 PM.

  • #3
    New Coder
    Join Date
    Sep 2002
    Location
    UK
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Y-STU-K; 07-04-2005 at 06:36 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •