2009/8/22 Keith <survivor_...@hotmail.com>:
> Thanks! Torben.
> I got the point now and it works! :-)
> I'm doing this because the statements of each cases is quite long, and I
> wish to have minimum coding without repetition.
Glad it works! I'm not sure how inverting the case statement helps you
minimize the code in each case. As both I and Adam showed, you can do
the same thing more efficiently (and IMHO much more readably) like
> "Lars Torben Wilson" <larstor...@gmail.com> wrote in message
>> 2009/8/20 Keith <survivor_...@hotmail.com>:
>>> I encounter a funny limitation here with switch case as below:
>>> The value for $sum is worked as expected for 1 to 8, but not for 0.
>>> When the $sum=0, the first case will be return, which is "sum=8".
>>> Is there any limitation / rules for switch case? Thanks for advice!
>> Hi Keith,
>> Try replacing 'switch($sum)' with 'switch(true)'.
>> Note that unless you have very good reasons for using a switch
>> statement like this, and know exactly why you're doing it, it's often
>> better just to use it in the normal fashion. i.e.:
>> switch ($sum)
>> case 8:
>> case 7:
>> case 6:
>> case 2:
>> case 1:
>> case 0:
>> Some people like the syntax you've presented but honestly, there's
>> usually a better way to do it.
>> This is also somewhat faster too, although you may only notice the
>> difference in very tight loops where you're counting every nanosecond.
>>> case ($sum==8):
>>> echo "sum=8";
>>> case ($sum==7 || $sum==6):
>>> echo "sum=7 or 6";
>>> case ($sum==2 || $sum==1):
>>> echo "sum=2 or 1";
>>> case 0:
>>> echo "sum=0";
>>> echo "sum=3/4/5";
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php