From: Ashley Sheridan on 24 Sep 2010 16:02 On Fri, 2010-09-24 at 15:44 -0400, Steve Staples wrote: > this would be the same as: > (commented below) > > On Fri, 2010-09-24 at 15:30 -0400, tedd wrote: > > At 2:23 PM -0400 9/24/10, Bob McConnell wrote: > > > > > >A switch works when a single test can dispatch all possible branches. If > > >you have a series of tests where each looks for a different subset of > > >conditions, you need an elseif. > > > > > >Bob McConnell > > > > Bob: > > > > Not so, O'wise one. > > > > This will work: > > > > switch(1) > > { > > case $a > $b: > if($a > $b) > > /* whatever > > break; > > > elseif ($c == 1) > > case $c == 1: > > /* whatever > > break; > > > elseif($d == 'this works') > > case $d == 'this works': > > /* whatever > > break; > > } > > Granted, it's not the normal way a switch works in some other > > languages, but it does work in PHP. :-) > > > > All you have to remember, and same as with this switch, is that the > first match, will stop processing the rest of the stuff. > > Steve. > > > > > > Cheers, > > > > tedd > > > > -- > > ------- > > http://sperling.com/ > > > > > Actually, processing only stops at the first break statement, so in this example, it would run through to case 3: $var = 1; switch($var) { case 1: { echo 1; } case 2: { echo 2; } case 3: { echo 3; break; } case 4: { echo 4; break; } default: { echo 'default'; } } This would display '123' in the output. Thanks, Ash http://www.ashleysheridan.co.uk
From: Ashley Sheridan on 24 Sep 2010 16:04 On Fri, 2010-09-24 at 15:54 -0400, Bob McConnell wrote: > From: tedd > > > At 2:23 PM -0400 9/24/10, Bob McConnell wrote: > >> > >>A switch works when a single test can dispatch all possible branches. > If > >>you have a series of tests where each looks for a different subset of > >>conditions, you need an elseif. > > > Not so, O'wise one. > > > > This will work: > > > > switch(1) > > { > > case $a > $b: > > /* whatever > > break; > > > > case $c == 1: > > /* whatever > > break; > > > > case $d == 'this works': > > /* whatever > > break; > > } > > > > Granted, it's not the normal way a switch works in some other > > languages, but it does work in PHP. :-) > > That is just so wrong, it can't actually be taken seriously. There is > simply no justification for such broken logic. > > Bob McConnell > I don't often use this type of logic, but I have used it before and it's served me well. Essentially, a switch is a glorified if statement, and I find them a lot nicer to read and write than a series of if/elseif blocks. Thanks, Ash http://www.ashleysheridan.co.uk
From: Ashley Sheridan on 24 Sep 2010 17:06 On Fri, 2010-09-24 at 16:08 -0400, Joshua Kehn wrote: > On Sep 24, 2010, at 4:04 PM, Ashley Sheridan wrote: > > > On Fri, 2010-09-24 at 15:54 -0400, Bob McConnell wrote: > > > >> From: tedd > >> > >>> At 2:23 PM -0400 9/24/10, Bob McConnell wrote: > >>>> > >>>> A switch works when a single test can dispatch all possible branches. > >> If > >>>> you have a series of tests where each looks for a different subset of > >>>> conditions, you need an elseif. > >> > >>> Not so, O'wise one. > >>> > >>> This will work: > >>> > >>> switch(1) > >>> { > >>> case $a > $b: > >>> /* whatever > >>> break; > >>> > >>> case $c == 1: > >>> /* whatever > >>> break; > >>> > >>> case $d == 'this works': > >>> /* whatever > >>> break; > >>> } > >>> > >>> Granted, it's not the normal way a switch works in some other > >>> languages, but it does work in PHP. :-) > >> > >> That is just so wrong, it can't actually be taken seriously. There is > >> simply no justification for such broken logic. > >> > >> Bob McConnell > >> > > > > > > I don't often use this type of logic, but I have used it before and it's > > served me well. Essentially, a switch is a glorified if statement, and I > > find them a lot nicer to read and write than a series of if/elseif > > blocks. > > > > > > Thanks, > > Ash > > http://www.ashleysheridan.co.uk > > > > > > Proper bracing style makes everything visually appealing. > > if(cond) > { > /* perform */ > } > else if(cond) > { > /* action */ > } > else if(cond) > { > /* if */ > } > else > { > /* applicable to cond */ > } > > Rather then > > if(cond){ > > } else if(cond) { > > } else if(cond) { > > } else { > > } > > which may look condensed but to me doesn't follow the program flow at all. > > Regards, > > -Josh That is my preferred way of coding anyway for several reasons as you can do this without causing any problems: //if(condition) { // code here } whereas if you do it with the variant of this style: // if(condition) { // code here //} You have to comment out two lines instead of one to achieve the same result, and you only save a byte (or two if the line ending is a carriage return and a line break) by putting the opening brace on the first line. The style made sense when it was used for code printouts and displaying on a text terminal, but in these modern days of GUI editors and the (mostly conceptual) paperless office, there seems little point. That is all a little off-topic though. Thanks, Ash http://www.ashleysheridan.co.uk
From: tedd on 25 Sep 2010 11:00 At 3:54 PM -0400 9/24/10, Bob McConnell wrote: >From: tedd > >> At 2:23 PM -0400 9/24/10, Bob McConnell wrote: >>> > >>A switch works when a single test can dispatch all possible branches. >If >>>you have a series of tests where each looks for a different subset of > >>conditions, you need an elseif. > >> Not so, O'wise one. >> >> This will work: >> >> switch(1) >> { >> case $a > $b: >> /* whatever >> break; >> >> case $c == 1: >> /* whatever >> break; >> >> case $d == 'this works': >> /* whatever >> break; >> } >> >> Granted, it's not the normal way a switch works in some other >> languages, but it does work in PHP. :-) > >That is just so wrong, it can't actually be taken seriously. There is >simply no justification for such broken logic. > >Bob McConnell I take it seriously. In fact, I think it's a very good method of making several different comparisons in one control structure. For me, it is easy to understand, document, and maintain. Obviously, your mileage varies. But besides the point, all I was showing was that your claim -- >A switch works when a single test can dispatch all possible >branches. If you have a series of tests where each looks for a >different subset of conditions, you need an elseif. -- was false. I don't need an elseif and never have. There has always been a way around using an elseif. The powers that be could boot that control and I would never miss it. Cheers, tedd -- ------- http://sperling.com/
From: tedd on 25 Sep 2010 11:01 At 9:04 PM +0100 9/24/10, Ashley Sheridan wrote: >I don't often use this type of logic, but I have used it before and >it's served me well. Essentially, a switch is a glorified if >statement, and I find them a lot nicer to read and write than a >series of if/elseif blocks. > >Thanks, >Ash Ash: Exactly my take. I wouldn't live next door to an ELSEIF even if it lived in a good neighborhood. :-) However, I always thought (maybe in error) that the switch control (CASE statement) was derived from the computed GOTO rather than from the three-way IF statement. One can make the argument that the ELSE IF statement first surfaced circa 1977 in FORTRAN 77 and the CASE statement came later in FORTRAN 90 circa 1991. But I know I was using computed GOTOs and GOSUBs long before then. In any event, to me the computed GOTO is more like the CASE statement than ELSE IF. Cheers, tedd -- ------- http://sperling.com/
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Heredocs, print_r() and newline "\n" and fnmatch() -curious failures ... Next: Array question |