0

I'm attempting to learn how to work with PHP switch statements. What I would like to do is check on a users role id.

  • If the user is a guest (role id of 1) then I want to run a specific query.
  • If the user is an editor (role id of 2) I don't want them to do anything.
  • If the user is an admin (role id of 3) I want them to run a specific query.
  • If the user is a webmaster (role id of 4) then I want them to run a specific query.

Am I forming this right and can I just break for the case 2 since I don't want them to do anything for an editor.

switch ($user_data->role_id)
{
    // User
    case 1:
        $user_data->characters = $this->character->get_many_by('user_id', $user_id);
        break;
    // Editor
    case 2:
        break;
    // Admins
    case 3:
        $user_data->characters = $this->character->get_many_by('user_id', $user_id);
        break;
    // Webmaster
    case 4:
        $user_data->characters = $this->character->get_all();
        break;
}
1
  • 1
    looks ok to me. do you get the results you want?
    – michi
    Aug 19, 2013 at 23:42

4 Answers 4

3

You could remove case 2 and combine 1 & 3 since they are the same and add a default to break

switch ($user_data->role_id){
    // User
    case 1:
    // Admins
    case 3:
        $user_data->characters = $this->character->get_many_by('user_id', $user_id);
        break;
    // Webmaster
    case 4:
        $user_data->characters = $this->character->get_all();
        break;
    default:
        break;
}
2
  • 1
    So for two cases to be the same you don't put a break in the first one. Aug 19, 2013 at 23:46
  • @user2576961 simple answer yes. It falls through kinda like if(case == 1 || case == 3) do this. later on if user and admin areas don't have same items to perform revert to previous switch w/ default.
    – Class
    Aug 19, 2013 at 23:48
1

I like Class' answer most. However, this is a typical case where your switch is allowed to take up some more space for clarity. You have defined user roles, and although users and admins may have things in common, they may not in the future. Case 2 is not useful now, but probably will be later on.

I'd add the default: break too for completeness. My answer:

switch ($user_data->role_id) {    
    case 1: // User
        $user_data->characters = $this->character->get_many_by('user_id', $user_id);
        break;    
    case 2: // Editor
        break;
   case 3: // Admin
        $user_data->characters = $this->character->get_many_by('user_id', $user_id);
        break;
    case 4: // Webmaster
        $user_data->characters = $this->character->get_all();
        break;
    default:
        break;
}
1

It seems good to me. For case 2 you should do it with the default!

switch ($user_data->role_id)
 {
 // User
 case 1:
     $user_data->characters = $this->character->get_many_by('user_id', $user_id);
     break;
 // Admins
 case 3:
     $user_data->characters = $this->character->get_many_by('user_id', $user_id);
     break;
 // Webmaster
 case 4:
     $user_data->characters = $this->character->get_all();
     break;
 default:

     break;
 //Editor and all the other non specifed
 }

So the default is for all the cases you didn't mentioned before.

1

If you don't want anything to happen if $user_data->role_id is 2 (Editor), then you can just take out your case 2: break; code.

Otherwise, your switch statement looks fine to me!

EDIT: I probably should have looked at the question more closely; as Class said, you can combine your User and Admin cases. You do this by saying

case 1: case 3:
    $user_data->characters = $this->character->get_many_by('user_id', $user_id);
    break;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.