php-general Digest 17 Nov 2012 15:20:11 -0000 Issue 8044

Topics (messages 319718 through 319723):

Re: Switch - Case Statement Questions
        319718 by: Iñigo Medina
        319719 by: tamouse mailing lists
        319722 by: Tim Streater
        319723 by: Stuart Dallas

How to get a PHP bug fixed?
        319720 by: Enumag
        319721 by: Iñigo Medina

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 ---

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 <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.

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


--- End Message ---
--- Begin Message ---
On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs <krebs....@gmail.com> wrote:
> Beside this it can be rewritten as
>
> switch ((int) (($count-1) / 7) {
>   case 0: // 1-7
>   case 1: // 8 - 14
>   default: // above 15
> }

Nice code refactoring :) Just a tad obscure for someone coming along
later, perhaps.

--- End Message ---
--- Begin Message ---
On 17 Nov 2012 at 01:33, Iñigo Medina <imed...@grosshat.com> wrote: 

> On Fri, 16 Nov 2012, Jim Giner wrote:

>> 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.

It may be an alternative, but it breaks the principle of least surprise. If I'm 
looking through someone's code, and I see a switch, I expect to see it used as 
Jim described. If the writer needs a different type of logic to make a 
decision, that is what the cascaded if-elseif-elseif construct is for, when the 
tests don't fall into a simple set-of-values choice.

The job of a programmer is not to be "clever" with a view to impress those who 
follow, but to achieve the desired outcome while at the same time making life 
easy for those who follow.

--
Cheers  --  Tim

--- End Message ---
--- Begin Message ---
On 17 Nov 2012, at 10:53, Tim Streater <t...@clothears.org.uk> wrote:

> On 17 Nov 2012 at 01:33, Iñigo Medina <imed...@grosshat.com> wrote: 
> 
>> On Fri, 16 Nov 2012, Jim Giner wrote:
> 
>>> 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.
> 
> It may be an alternative, but it breaks the principle of least surprise. If 
> I'm looking through someone's code, and I see a switch, I expect to see it 
> used as Jim described. If the writer needs a different type of logic to make 
> a decision, that is what the cascaded if-elseif-elseif construct is for, when 
> the tests don't fall into a simple set-of-values choice.

I don't see how it breaks the "principle of least surprise" (which is a new one 
on me. KISS, yes. Least surprise? Wassat?). If you consider that behaviour to 
be surprising the don't ever go near Perl.

A switch statement is basically saying "take this value and compare it to each 
case below in the order they're presented until one matches, then execute that 
block of code until you hit a break which will jump execution out of the switch 
statement." I don't see how making "this value" true or false makes it any more 
surprising than if it's "squirlookle."

> The job of a programmer is not to be "clever" with a view to impress those 
> who follow, but to achieve the desired outcome while at the same time making 
> life easy for those who follow.

I don't consider that usage of the switch statement to be "clever." It's a 
valid usage of the statement which gives it a lot more power. Specifically the 
ability to have the code for a given case deliberately fall through to the next 
case block is worth any price of admission. And finally, in my opinion, it's 
far cleaner than a succession of if/elseif/else statements, and ultimately gets 
compiled to very similar opcodes.

I agree that the job of a programmer is to balance providing a fast, efficient, 
correct solution and minimising maintenance issues. I see neither side being 
compromised by using a switch statement in this manner, in fact I see both 
sides potentially getting a boost. Just because the usage is unfamiliar or even 
surprising to you doesn't mean it's wrong.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
Hi, there is a bug I'd like to be fixed and even a patch is available. But
there is still no reaction at all after 2 years. What else can I do to get
the bug fixed?

https://bugs.php.net/bug.php?id=45351 - patch available from 2010-06-13
https://bugs.php.net/bug.php?id=48724 - patch available from 2012-04-13

--- End Message ---
--- Begin Message ---

On Sat, 17 Nov 2012, Enumag wrote:

Hi, there is a bug I'd like to be fixed and even a patch is available. But
there is still no reaction at all after 2 years. What else can I do to get
the bug fixed?

https://bugs.php.net/bug.php?id=45351 - patch available from 2010-06-13
https://bugs.php.net/bug.php?id=48724 - patch available from 2012-04-13

It looks like there is a thread about that on the PHP Bugzilla, but it is not
admitted as a patch to be accepted. Maybe you might try by GitHub, making
a pull request.

iñ


--- End Message ---

Reply via email to