php-general Digest 17 Nov 2012 01:00:54 -0000 Issue 8043

Topics (messages 319712 through 319717):

re:Switch - Case Statement Questions
        319712 by: Omar Muhsin
        319713 by: Ken Robinson
        319716 by: Iñigo Medina

Re: Switch - Case Statement Questions
        319714 by: Tim Streater
        319715 by: Sebastian Krebs
        319717 by: Jim Giner

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
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;
//        }


Does anyone know the answer why using the Switch it always execute the first 
case ?

Many thanks

Omar

--- End Message ---
--- Begin Message ---
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;
        }

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


--- End Message ---
--- Begin 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


--- End Message ---
--- Begin Message ---
On 16 Nov 2012 at 12:10, Omar Muhsin <mrfroa...@gmail.com> 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)

You don't need the count<=14 part as you will *only* get there if $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;
> //        }
>
>
> Does anyone know the answer why using the Switch it always execute the first
> case ?

As has been pointed out you need switch(true).

That's a strange way of writing a switch, IMO. You should be using the if 
version.

--
Cheers  --  Tim

--- End Message ---
--- Begin Message ---
2012/11/16 Tim Streater <t...@clothears.org.uk>

> On 16 Nov 2012 at 12:10, Omar Muhsin <mrfroa...@gmail.com> 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)
>
> You don't need the count<=14 part as you will *only* get there if
> $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;
> > //        }
> >
> >
> > Does anyone know the answer why using the Switch it always execute the
> first
> > case ?
>
> As has been pointed out you need switch(true).
>
> That's a strange way of writing a switch, IMO. You should be using the if
> version.
>

Hi,

I've seen looooong discussions about this ;) I think this one is a example,
where 'switch(true)' is a legit expression, because switch-statements
define a construct, where the interpreter should select one of multiple
similar cases. This one seems legit, because they are very similar ;) It
says "In case of $count look like X do this, or $count look like Y do that,
or $count look like Z do something else (and so on)". For such  I find the
if-elseif-elseif-elseif-elseif-else-construction quite confusing.

Beside this it can be rewritten as

switch ((int) (($count-1) / 7) {
  case 0: // 1-7
  case 1: // 8 - 14
  default: // above 15
}


Regards,
Sebastian


>
> --
> Cheers  --  Tim
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>



-- 
github.com/KingCrunch

--- End Message ---
--- Begin Message ---
On 11/16/2012 12:38 PM, Tim Streater wrote:
On 16 Nov 2012 at 12:10, Omar Muhsin <mrfroa...@gmail.com> 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)

You don't need the count<=14 part as you will *only* get there if $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;
//        }


Does anyone know the answer why using the Switch it always execute the first
case ?

As has been pointed out you need switch(true).

That's a strange way of writing a switch, IMO. You should be using the if 
version.

--
Cheers  --  Tim

Maybe I'm way out of touch, but when I look at php.net for the syntax of the switch statement I see:
switch($var){
        case (value):
                (do something)
        case (other value):
                (do something else)
}

IN your example, you are using a switch syntax that is nothing like that. I think that your "case " statements are not actually evaluating the value of $count, but are themselves evaluating to a true value that has nothing to do with $count.

Here's the manual reference:
http://us.php.net/manual/en/control-structures.switch.php

--- End Message ---

Reply via email to