Re: [PHP] Is there limitation for switch case: argument's value?
Oh I see. I missed out one thing too. There is no need to put the same code under both case 7 & 6 separately, but just under case 6 will do. This is simpler in fact! Thanks Tedd and Torben! I'll fix to this style from now onwards. "tedd" wrote in message news:p06240800c6b84328c...@[192.168.1.100]... At 9:44 PM -0700 8/22/09, Lars Torben Wilson wrote: Hi Keith, 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 this: switch ($sum) { case 8: break; case 7: case 6: break; case 2: case 1: break; case 0: break; default: break; } Additionally, I would argue there's nothing different below other than it's even more easier to read and understand: switch ($sum) { case 0: break; case 1: case 2: break; case 6: case 7: break; case 8: break; default: break; } Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there limitation for switch case: argument's value?
At 9:44 PM -0700 8/22/09, Lars Torben Wilson wrote: Hi Keith, 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 this: switch ($sum) { case 8: break; case 7: case 6: break; case 2: case 1: break; case 0: break; default: break; } Additionally, I would argue there's nothing different below other than it's even more easier to read and understand: switch ($sum) { case 0: break; case 1: case 2: break; case 6: case 7: break; case 8: break; default: break; } Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there limitation for switch case: argument's value?
Aargh. Slipped on the trigger there--premature Send. See below for what I meant to send: 2009/8/22 Lars Torben Wilson : > 2009/8/22 Keith : >> 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. Hi Keith, 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 more readably) like this: switch ($sum) { case 8: echo "The sum is 8"; break; case 7: case 6: echo "The sum is 7 or 6"; break; case 2: case 1: echo "The sum is 2 or 1"; break; case 0: echo "The sum is 0"; break; default: echo "The sum is 3, 4, or 5"; break; } And if the code is getting so long that it's getting unwieldy, you might want to look into breaking it out into separate functions. Anyway, it's not a huge thing; personally though I feel that it's a syntax that if ever used, should only be used for very good reasons. Most of the uses I've seen of it, however, fall into the "overly clever" category and add nothing in the end to the quality of the code. That's totally a judgement call on my part, of course. :) Again, glad it works, and keep on coding! Regards, Torben >> "Lars Torben Wilson" wrote in message >> news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com... >>> >>> 2009/8/20 Keith : Hi, 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! Keith >>> >>> 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: >>> break; >>> case 7: >>> case 6: >>> break; >>> case 2: >>> case 1: >>> break; >>> case 0: >>> break; >>> default: >>> break; >>> } >>> >>> 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. >>> >>> >>> Regards, >>> >>> Torben >>> $sum=0; switch($sum) { case ($sum==8): echo "sum=8"; break; case ($sum==7 || $sum==6): echo "sum=7 or 6"; break; case ($sum==2 || $sum==1): echo "sum=2 or 1"; break; case 0: echo "sum=0"; break; default: echo "sum=3/4/5"; break; } -- 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
Re: [PHP] Is there limitation for switch case: argument's value?
2009/8/22 Keith : > 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. Hi Keith, 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 this: switch ($sum) { case 8: break; case 7: case 6: break; case 2: case 1: break; case 0: break; default: break; } > "Lars Torben Wilson" wrote in message > news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com... >> >> 2009/8/20 Keith : >>> >>> Hi, >>> 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! >>> >>> Keith >> >> 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: >> break; >> case 7: >> case 6: >> break; >> case 2: >> case 1: >> break; >> case 0: >> break; >> default: >> break; >> } >> >> 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. >> >> >> Regards, >> >> Torben >> >>> $sum=0; >>> switch($sum) >>> { >>> case ($sum==8): >>> echo "sum=8"; >>> break; >>> case ($sum==7 || $sum==6): >>> echo "sum=7 or 6"; >>> break; >>> case ($sum==2 || $sum==1): >>> echo "sum=2 or 1"; >>> break; >>> case 0: >>> echo "sum=0"; >>> break; >>> default: >>> echo "sum=3/4/5"; >>> break; >>> } >>> -- >>> 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
Re: [PHP] Is there limitation for switch case: argument's value?
Hahaha! Actually this is my first time coding, so I still adapting the correct way to do it. FYI, for the sake of minimizing the coding and viewing space occupied of the code, I even replace the switch block with array if the cases are just merely selecting pool of variables/categories/parameters! I'm still learning the best practice for coding. :-) ""Daevid Vincent"" wrote in message news:fef58898dc3544e3872458cd64661...@mascorp.com... Whoa! I didn't even know you could use a switch statement like that. In 20 years of coding, I've never ever used a switch like that first "if/else" style. PHP never ceases to amaze me in it's flexibility (and ability to shoot yourself in the foot ;-p ) And remember, all your base are belong to Adam. -Original Message- From: Adam Randall [mailto:randa...@gmail.com] Sent: Thursday, August 20, 2009 11:20 PM To: Keith Cc: php-general@lists.php.net Subject: Re: [PHP] Is there limitation for switch case: argument's value? I've never understood why people use switch statements like this as in reality you are using it more like an if/else block, but anyway. The reason why your code is not working is that you are passing into the switch the value of 0, or false, which means that when any of those $sum == N conditionals comes up as false (say $sum == 8 ), then that is what is returned because they match up. In PHP's eyes, 0 == false in switch statements. To fix your code, change the switch( $sum ) to switch( true ): switch( true ) { case ($sum == 8): echo "sum=8\n"; break; case ($sum == 7 || $sum == 6): echo "sum=7 or 6\n"; break; case ($sum == 2 || $sum == 1): echo "sum=2 or 1\n"; break; case ($sum == 0): echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Or, write your switch like this: switch( $sum ) { case 8: echo "sum=8\n"; break; case 6: case 7: echo "sum=7 or 6\n"; break; case 1: case 2: echo "sum=2 or 1\n"; break; case 0: echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Regards, Adam. On Thu, Aug 20, 2009 at 8:40 PM, Keith wrote: > Hi, > 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! > > Keith > > $sum=0; > switch($sum) > { > case ($sum==8): > echo "sum=8"; > break; > case ($sum==7 || $sum==6): > echo "sum=7 or 6"; > break; > case ($sum==2 || $sum==1): > echo "sum=2 or 1"; > break; > case 0: > echo "sum=0"; > break; > default: > echo "sum=3/4/5"; > break; > } > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Adam Randall http://www.xaren.net AIM: blitz574 -- 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
Re: [PHP] Is there limitation for switch case: argument's value?
Thanks! Adam. It works now! Actually I thought that switch block is compatible with if/elseif/else block but more efficient if the cases is >3. Because I like to have short coding without repetition, so I group the similar cases together. Thanks for your explanation! I know where is my mistake already and I've learnt a lesson today! "Adam Randall" wrote in message news:b6d6f8360908202319n240bba5al6c02edb6e890b...@mail.gmail.com... I've never understood why people use switch statements like this as in reality you are using it more like an if/else block, but anyway. The reason why your code is not working is that you are passing into the switch the value of 0, or false, which means that when any of those $sum == N conditionals comes up as false (say $sum == 8 ), then that is what is returned because they match up. In PHP's eyes, 0 == false in switch statements. To fix your code, change the switch( $sum ) to switch( true ): switch( true ) { case ($sum == 8): echo "sum=8\n"; break; case ($sum == 7 || $sum == 6): echo "sum=7 or 6\n"; break; case ($sum == 2 || $sum == 1): echo "sum=2 or 1\n"; break; case ($sum == 0): echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Or, write your switch like this: switch( $sum ) { case 8: echo "sum=8\n"; break; case 6: case 7: echo "sum=7 or 6\n"; break; case 1: case 2: echo "sum=2 or 1\n"; break; case 0: echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Regards, Adam. On Thu, Aug 20, 2009 at 8:40 PM, Keith wrote: Hi, 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! Keith $sum=0; switch($sum) { case ($sum==8): echo "sum=8"; break; case ($sum==7 || $sum==6): echo "sum=7 or 6"; break; case ($sum==2 || $sum==1): echo "sum=2 or 1"; break; case 0: echo "sum=0"; break; default: echo "sum=3/4/5"; break; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Adam Randall http://www.xaren.net AIM: blitz574 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there limitation for switch case: argument's value?
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. "Lars Torben Wilson" wrote in message news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com... 2009/8/20 Keith : Hi, 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! Keith 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: break; case 7: case 6: break; case 2: case 1: break; case 0: break; default: break; } 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. Regards, Torben $sum=0; switch($sum) { case ($sum==8): echo "sum=8"; break; case ($sum==7 || $sum==6): echo "sum=7 or 6"; break; case ($sum==2 || $sum==1): echo "sum=2 or 1"; break; case 0: echo "sum=0"; break; default: echo "sum=3/4/5"; break; } -- 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
RE: [PHP] Is there limitation for switch case: argument's value?
Whoa! I didn't even know you could use a switch statement like that. In 20 years of coding, I've never ever used a switch like that first "if/else" style. PHP never ceases to amaze me in it's flexibility (and ability to shoot yourself in the foot ;-p ) And remember, all your base are belong to Adam. > -Original Message- > From: Adam Randall [mailto:randa...@gmail.com] > Sent: Thursday, August 20, 2009 11:20 PM > To: Keith > Cc: php-general@lists.php.net > Subject: Re: [PHP] Is there limitation for switch case: > argument's value? > > I've never understood why people use switch statements like this as in > reality you are using it more like an if/else block, but anyway. > > The reason why your code is not working is that you are passing into > the switch the value of 0, or false, which means that when any of > those $sum == N conditionals comes up as false (say $sum == 8 ), then > that is what is returned because they match up. In PHP's eyes, 0 == > false in switch statements. > > To fix your code, change the switch( $sum ) to switch( true ): > > switch( true ) > { > case ($sum == 8): > echo "sum=8\n"; > break; > case ($sum == 7 || $sum == 6): > echo "sum=7 or 6\n"; > break; > case ($sum == 2 || $sum == 1): > echo "sum=2 or 1\n"; > break; > case ($sum == 0): > echo "sum=0\n"; > break; > default: > echo "sum=3/4/5\n"; > break; > } > > Or, write your switch like this: > > switch( $sum ) > { > case 8: > echo "sum=8\n"; > break; > case 6: > case 7: > echo "sum=7 or 6\n"; > break; > case 1: > case 2: > echo "sum=2 or 1\n"; > break; > case 0: > echo "sum=0\n"; > break; > default: > echo "sum=3/4/5\n"; > break; > } > > Regards, > > Adam. > > On Thu, Aug 20, 2009 at 8:40 PM, > Keith wrote: > > Hi, > > 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! > > > > Keith > > > > $sum=0; > > switch($sum) > > { > > case ($sum==8): > > echo "sum=8"; > > break; > > case ($sum==7 || $sum==6): > > echo "sum=7 or 6"; > > break; > > case ($sum==2 || $sum==1): > > echo "sum=2 or 1"; > > break; > > case 0: > > echo "sum=0"; > > break; > > default: > > echo "sum=3/4/5"; > > break; > > } > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- > Adam Randall > http://www.xaren.net > AIM: blitz574 > > -- > 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
Re: [PHP] Is there limitation for switch case: argument's value?
2009/8/20 Keith : > Hi, > 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! > > Keith 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: break; case 7: case 6: break; case 2: case 1: break; case 0: break; default: break; } 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. Regards, Torben > $sum=0; > switch($sum) > { > case ($sum==8): > echo "sum=8"; > break; > case ($sum==7 || $sum==6): > echo "sum=7 or 6"; > break; > case ($sum==2 || $sum==1): > echo "sum=2 or 1"; > break; > case 0: > echo "sum=0"; > break; > default: > echo "sum=3/4/5"; > break; > } > -- > 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
Re: [PHP] Is there limitation for switch case: argument's value?
I've never understood why people use switch statements like this as in reality you are using it more like an if/else block, but anyway. The reason why your code is not working is that you are passing into the switch the value of 0, or false, which means that when any of those $sum == N conditionals comes up as false (say $sum == 8 ), then that is what is returned because they match up. In PHP's eyes, 0 == false in switch statements. To fix your code, change the switch( $sum ) to switch( true ): switch( true ) { case ($sum == 8): echo "sum=8\n"; break; case ($sum == 7 || $sum == 6): echo "sum=7 or 6\n"; break; case ($sum == 2 || $sum == 1): echo "sum=2 or 1\n"; break; case ($sum == 0): echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Or, write your switch like this: switch( $sum ) { case 8: echo "sum=8\n"; break; case 6: case 7: echo "sum=7 or 6\n"; break; case 1: case 2: echo "sum=2 or 1\n"; break; case 0: echo "sum=0\n"; break; default: echo "sum=3/4/5\n"; break; } Regards, Adam. On Thu, Aug 20, 2009 at 8:40 PM, Keith wrote: > Hi, > 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! > > Keith > > $sum=0; > switch($sum) > { > case ($sum==8): > echo "sum=8"; > break; > case ($sum==7 || $sum==6): > echo "sum=7 or 6"; > break; > case ($sum==2 || $sum==1): > echo "sum=2 or 1"; > break; > case 0: > echo "sum=0"; > break; > default: > echo "sum=3/4/5"; > break; > } > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Adam Randall http://www.xaren.net AIM: blitz574 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Is there limitation for switch case: argument's value?
Hi, 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! Keith $sum=0; switch($sum) { case ($sum==8): echo "sum=8"; break; case ($sum==7 || $sum==6): echo "sum=7 or 6"; break; case ($sum==2 || $sum==1): echo "sum=2 or 1"; break; case 0: echo "sum=0"; break; default: echo "sum=3/4/5"; break; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php