Re: re:Switch - Case Statement Questions

From: Date: Fri, 16 Nov 2012 19:24:44 +0000
Subject: Re: re:Switch - Case Statement Questions
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-319716@lists.php.net to get a copy of this message
On Fri, 16 Nov 2012, Ken Robinson wrote:
At 07:10 AM 11/16/2012, Omar Muhsin wrote:
Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}:
        if ($count > 14)
            $boxes = 3;
        elseif($count > 7 && $count <= 14)
            $boxes = 2;
        else
            $boxes = 1;
2. Using Switch {ALWAYS FIRST CASE!!!}
//        $boxes = 1;
//        switch ($count) {
//            case ($count > 14):
//                $boxes = 3;
//                break;
//            case ($count > 7 && $count <= 14):
//                $boxes = 2;
//                break;
//            case ($count <= 7):
//            default :
//                $boxes = 1;
//                break;
//        }
For the switch statement to work, you need to use "switch (true)"
      $boxes = 1;
      switch (true) {
           case ($count > 14):
               $boxes = 3;
               break;
          case ($count > 7 && $count <= 14):
               $boxes = 2;
              break;
          case ($count <= 7):
           default :
              $boxes = 1;
              break;
       }
I understand you use switch there for answering the question 'which of the n following questions is true'. But how do you evaluate $count? iñ
BTW, you don't need the "break" statement at the end of the last case and you don't really need the "case ($count <= 7)" since that condition is what remains anyway. Ken -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#319716) next »