Codeigniter,使用if语句..如何让它更简单?

时间:2018-02-08 07:30:58

标签: php codeigniter

Codeigniter,使用if语句..如何使其更简单?

if($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="x")
{
  Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="y")
{
  Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="z")
{
  Do action
}
else
{
 done 
}

5 个答案:

答案 0 :(得分:3)

您可以使用开关

  if($this->session->userdata('logged_in')!="" ) {

     switch ($this->session->userdata('stts'){
        case "x":
          Do action for x 
          break;
        case "y":
          Do action for y
          break;
        case "z":
          Do action for z 
          break;   
        default:
          Do action for othes 
          break;   
     }

  } else {
   // done

 }

答案 1 :(得分:2)

你可以使用这样的开关案例:

if($this->session->userdata('logged_in')!=""){
        switch($this->session->userdata('stts')){
            case "x":
                // Do action
                break;
            case "y":
                // Do action
                break;
            case "z":
                // Do action
                break;
            default:
                // Do action
                break;

        }

    }
else{
//if session not set
}

希望这有帮助!

答案 2 :(得分:1)

使用开关盒可以更美化它。

if($this->session->userdata('logged_in')!="")
{
   switch($this->session->userdata('stts'))
   {
       case "x":
             //do some thing
         break;
       case "y":
             //do some thing
         break;
       case "z":
             //do some thing
         break;
       default:
           //do some thing
   }

} else {
  // else condition
}

答案 3 :(得分:1)

if ($this->session->userdata('logged_in') != "" && in_array($this->session->userdata('stts'), ['x', 'y', 'z']))
{
    //Do action
}
else
{
    //done
}

答案 4 :(得分:0)

您可以使用简单的if / else和switch语句:

if ($this->session->userdata('logged_in') != "") {
    switch ($this->session->userdata('stts')) {
        case 'x':
            // do action
            break;
        case 'y':
            // do action
            break;
        case 'z':
            // do action
            break;
        default:
            // if neither x,y,z are stts
            break;
    }
} else {
    // done
}