Re: Re: Switch - Case Statement Questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/16/2012 8:33 PM, Iñigo Medina wrote:

On Fri, 16 Nov 2012, Jim Giner wrote:

On 11/16/2012 12:38 PM, Tim Streater wrote:
On 16 Nov 2012 at 12:10, Omar Muhsin <mrfroasty@xxxxxxxxx> 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.

That `switch` is evaluating to true each case. It's an alternative of using
if-elseif structure.

iñ


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

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


My point was that the OP is using a widely different syntax for a Switch statement, one that I don't understand at all. According to the Manual his statement should be:

>>>> //        switch ($count) {
>>>> //            case (> 14):
>>>> //                $boxes = 3;
>>>> //                break;
>>>> //            case (> 7):
>>>> //                $boxes = 2;
>>>> //                break;
>>>> //            default :
>>>> //                $boxes = 1;
>>>> //                break;
>>>> //        }


To answer Iñigo's comment - the OP's version is very much like an If-ElseIF structure, and nothing like a Switch.

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux