Re: [PHP] switch vs elseif
At 2:18 PM -0600 1/13/09, Micah Gersten wrote: Jochem Maas wrote: switch (true) { case ($x === $y): // something > break; This is a misuse of the switch statement. Switch is meant to compare values to a single variable as stated on the manual page: http://us2.php.net/switch Thank you, Micah Gersten Micah: I disagree. You may use a switch statement as Jochem demonstrated -- I do it all the time. Just because you think the value within the switch is supposed to be the value examined doesn't mean that doing it otherwise is wrong -- it's just a different way of using that statement. 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] switch vs elseif
I regret that I don't recall who made the brilliant observation that programmers spend the majority of their time *reading* code (their own or others) as opposed to *writing* code. So, I make it a point to try to make my code easily scannable. I only and always use the switch construct when evaluating the value of a single variable. The switch then tells me that at a glance when I'm reading code, and I don't have to plow through a bunch of elseif clauses to get the big picture of what the code is doing. The bottom line for me on this issue is the "bottom line" - productivity. Kirk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, 2009-01-13 at 22:12 +0100, Jochem Maas wrote: > Robert Cummings schreef: > > On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: > >> Ashley Sheridan schreef: > >>> On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info > on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything > else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > > >>> And a switch is a lot neater for dealing with these sorts of things. I > >>> tend never to use if...elseif's at all, and use switches. Like Rob said, > >>> you can fall into further cases below, and it's very simple to add more > >>> at a later date. There is one place where an if...elseif would work and > >>> a switch could not, and that is where you were performing lots of > >>> different logic tests on different variables. > >> switch (true) { > >>case ($x === $y): > >>// something > >>break; > >> > >>case ($a != $b): > >>// something > >>break; > >> > >>case (myFunc()): > >>// something > >>break; > >> > >>case ($my->getChild()->hasEatenBeans()): > >>// something > >>break; > >> } > >> > >> evil ... but it works. > > > > What is your intent if both $x === $y and $a != $b? From the above > > example only the code for $x === y will execute. > > no shit. that would be the intention too :-) > > > This is hideous from a > > readability point of view since the first case and second case are > > matches, but only the first will get executed. This is better > > represented using an if/elseif/else structure to indicate the exclusion > > of later expressions. > > that is just a matter of opinion. it's no different to an > if/elseif/elseif/elseif > block functionally ... you just don't like the way it looks No, it's not just a matter of functionality in this case. It malforms the semantic meaning of the named expressions so that they no longer provide clearly readable meaning. > ... I'd point out though that you had no trouble determining the > code's intention correctly, and I'm sure you didn't stare at it too > long I'm special though ;) > ... so I'd hazard to say it's not *that* illegible. Yes, that's right, it's a hazard alright. A hazard to avoid by any self-respecting developer who's not competing in a code obfuscation contest :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Micah Gersten wrote: > Jochem Maas wrote: >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. >> >> >> >> > This is a misuse of the switch statement. Switch is meant to compare > values to a single variable as stated on the manual page: > http://us2.php.net/switch > > Thank you, > Micah Gersten > onShore Networks > Internal Developer > http://www.onshore.com > Actually, if you read the link you posted, the first paragraph: "In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for." Notice the (or expression), which I believe true is one. Also, down the page: "The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type." I believe boolean is also one. I would point out however that the switch does a loose comparison, so this would be different: switch (true) { case (strpos("shawn", "s")): //actually returns 0 so is false break; case (strpos("shawn", "s") !== false): //returns true break; } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Nathan Rixham schreef: > Jochem Maas wrote: >> Micah Gersten schreef: >>> Jochem Maas wrote: switch (true) { > > should be switch(false) { > > :-) > it could be either depending on your needs, no? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Robert Cummings schreef: > On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: >> Ashley Sheridan schreef: >>> On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank >>> And a switch is a lot neater for dealing with these sorts of things. I >>> tend never to use if...elseif's at all, and use switches. Like Rob said, >>> you can fall into further cases below, and it's very simple to add more >>> at a later date. There is one place where an if...elseif would work and >>> a switch could not, and that is where you were performing lots of >>> different logic tests on different variables. >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. > > What is your intent if both $x === $y and $a != $b? From the above > example only the code for $x === y will execute. no shit. that would be the intention too :-) > This is hideous from a > readability point of view since the first case and second case are > matches, but only the first will get executed. This is better > represented using an if/elseif/else structure to indicate the exclusion > of later expressions. that is just a matter of opinion. it's no different to an if/elseif/elseif/elseif block functionally ... you just don't like the way it looks ... I'd point out though that you had no trouble determining the code's intention correctly, and I'm sure you didn't stare at it too long ... so I'd hazard to say it's not *that* illegible. > Cheers, > Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Jochem Maas wrote: Micah Gersten schreef: Jochem Maas wrote: switch (true) { should be switch(false) { :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Micah Gersten schreef: > Jochem Maas wrote: >> switch (true) { >> case ($x === $y): >> // something >> break; >> >> case ($a != $b): >> // something >> break; >> >> case (myFunc()): >> // something >> break; >> >> case ($my->getChild()->hasEatenBeans()): >> // something >> break; >> } >> >> evil ... but it works. >> >> >> >> > This is a misuse of the switch statement. Switch is meant to compare > values to a single variable as stated on the manual page: > http://us2.php.net/switch bla bla, I did say it was evil, for a more thorough rebuttal a defer to Richard Lynch. > > Thank you, > Micah Gersten > onShore Networks > Internal Developer > http://www.onshore.com > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
I think if they didn't want us to use expressions in the case, then they wouldn't have put support into the language for that. I daresay you are reading more into the text than was intended... I certainly have found switch(true) with complex expressions for case quite handy and very clear code on several occasions. ymmv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Jochem Maas wrote: > switch (true) { > case ($x === $y): > // something > break; > > case ($a != $b): > // something > break; > > case (myFunc()): > // something > break; > > case ($my->getChild()->hasEatenBeans()): > // something > break; > } > > evil ... but it works. > > > > This is a misuse of the switch statement. Switch is meant to compare values to a single variable as stated on the manual page: http://us2.php.net/switch Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, 2009-01-13 at 15:50 +0100, Jochem Maas wrote: > Ashley Sheridan schreef: > > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > >> I've googled, and found some confusing answers. > >> I've tried searching the history of the news group, and only found info on > >> switch or elseif seperately. :( > >> > >> Strictly from a performance stand point, not preference or anything else, > >> is > >> there a benefit of one over the other? > >> > >> > >> for($i=0;$i<3;$i++){ > >> switch($i){ > >> case 0: > >> header pg1 code > >> break; > >> case 1: > >> header pg2 code > >> break; > >> case 3: > >> header pg3 code > >> break; > >> }; > >> }; > >> > >> > >> or would that be better served using an if...elseif structure? > >> > >> Frank > >> > >> > >> > > And a switch is a lot neater for dealing with these sorts of things. I > > tend never to use if...elseif's at all, and use switches. Like Rob said, > > you can fall into further cases below, and it's very simple to add more > > at a later date. There is one place where an if...elseif would work and > > a switch could not, and that is where you were performing lots of > > different logic tests on different variables. > > switch (true) { > case ($x === $y): > // something > break; > > case ($a != $b): > // something > break; > > case (myFunc()): > // something > break; > > case ($my->getChild()->hasEatenBeans()): > // something > break; > } > > evil ... but it works. What is your intent if both $x === $y and $a != $b? From the above example only the code for $x === y will execute. This is hideous from a readability point of view since the first case and second case are matches, but only the first will get executed. This is better represented using an if/elseif/else structure to indicate the exclusion of later expressions. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Tue, Jan 13, 2009 at 9:50 AM, Jochem Maas wrote: > switch (true) { >case ($x === $y): >// something >break; > >case ($a != $b): >// something >break; > >case (myFunc()): >// something >break; > >case ($my->getChild()->hasEatenBeans()): >// something >break; > } > > evil ... but it works. > > PS - hi, people happy new year (or whatever) You too! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
Ashley Sheridan schreef: > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >> switch($i){ >> case 0: >> header pg1 code >> break; >> case 1: >> header pg2 code >> break; >> case 3: >> header pg3 code >> break; >> }; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> > And a switch is a lot neater for dealing with these sorts of things. I > tend never to use if...elseif's at all, and use switches. Like Rob said, > you can fall into further cases below, and it's very simple to add more > at a later date. There is one place where an if...elseif would work and > a switch could not, and that is where you were performing lots of > different logic tests on different variables. switch (true) { case ($x === $y): // something break; case ($a != $b): // something break; case (myFunc()): // something break; case ($my->getChild()->hasEatenBeans()): // something break; } evil ... but it works. PS - hi, people happy new year (or whatever) ... it's not that I'm dead ... I just can't be arsed atm. > Aside from that, I think > any speed benefit one would have over the other would be marginal. > > > Ash > www.ashleysheridan.co.uk > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 5:12 PM -0500 1/12/09, Robert Cummings wrote: On Mon, 2009-01-12 at 17:09 -0500, tedd wrote: At 4:57 PM -0500 1/12/09, Robert Cummings wrote: >No, that's DIFFERENT from what I wrote. Go back and re-read. > >:) I did and reread again -- it looks different. I'm simply talking about the spacing. No matter though, it's the same code. NO, it's not. Obviously my original comment has hit true in your case and it's not as readable as one might like to be self explanatory ;) Cheers, Rob. -- Okay. 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] switch vs elseif
On Mon, 2009-01-12 at 17:09 -0500, tedd wrote: > At 4:57 PM -0500 1/12/09, Robert Cummings wrote: > >No, that's DIFFERENT from what I wrote. Go back and re-read. > > > >:) > > I did and reread again -- it looks different. I'm simply talking > about the spacing. > > No matter though, it's the same code. NO, it's not. Obviously my original comment has hit true in your case and it's not as readable as one might like to be self explanatory ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 4:57 PM -0500 1/12/09, Robert Cummings wrote: No, that's DIFFERENT from what I wrote. Go back and re-read. :) I did and reread again -- it looks different. I'm simply talking about the spacing. No matter though, it's the same code. 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] switch vs elseif
On Mon, 2009-01-12 at 16:49 -0500, tedd wrote: > At 3:32 PM -0500 1/12/09, Robert Cummings wrote: > >One has to wonder about the readability of the case version though since > >one may not notice immediately the missing break statement. > > > >Cheers, > >Rob. > > Yes, but that's because of the way you wrote it -- consider this: > > switch( $foo ) > { > case 0: > // something > break; > > case 2: > case 3: > // something else > // something elser > break; > > default: > // something defaulty > } > ?> > > If you group the case's together, then there's no problem > understanding what happens in case 2 or 3. > > Cheers, > > tedd > -- > --- > http://sperling.com http://ancientstones.com http://earthstones.com > I'd go further and enclose the code meant to execute in each case inside braces. Won't make a difference to the outcome of the code, but for some code editors (KATE for example), it helps to be able to close blocks of code so that it is hidden from view. DreamWeaver fans are missing out on this still I believe. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
tedd wrote: At 3:32 PM -0500 1/12/09, Robert Cummings wrote: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. Yes, but that's because of the way you wrote it -- consider this: I'm sure that was completely intentional. If you group the case's together, then there's no problem understanding what happens in case 2 or 3. What happens if (like in Rob's example) I want a bunch of extra stuff to happen for case 2 but not 3? You can either group them like you have and have: case 2: case 3: if ($foo == 2) { // do extra stuff } ... break; or not put them under each other: case 2: // do extra stuff case 3: ... break; -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 16:49 -0500, tedd wrote: > At 3:32 PM -0500 1/12/09, Robert Cummings wrote: > >One has to wonder about the readability of the case version though since > >one may not notice immediately the missing break statement. > > > >Cheers, > >Rob. > > Yes, but that's because of the way you wrote it -- consider this: > > switch( $foo ) > { > case 0: > // something > break; > > case 2: > case 3: > // something else > // something elser > break; > > default: > // something defaulty > } > ?> > > If you group the case's together, then there's no problem > understanding what happens in case 2 or 3. No, that's DIFFERENT from what I wrote. Go back and re-read. :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
At 3:32 PM -0500 1/12/09, Robert Cummings wrote: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. Yes, but that's because of the way you wrote it -- consider this: If you group the case's together, then there's no problem understanding what happens in case 2 or 3. 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] switch vs elseif
At 3:15 PM -0500 1/12/09, Frank Stanovcak wrote: I've googled, and found some confusing answers. I've tried searching the history of the news group, and only found info on switch or elseif seperately. :( Strictly from a performance stand point, not preference or anything else, is there a benefit of one over the other? for($i=0;$i<3;$i++){ switch($i){ case 0: header pg1 code break; case 1: header pg2 code break; case 3: header pg3 code break; }; }; or would that be better served using an if...elseif structure? Frank elseif!!! Arr !!! I guess that today is my day to get myself in all sorts of trouble. I have never used elseif -- and can't stand it, tedd's rules. if ($options > 2) { echo('use switch'); { else { echo('use if'); } Reason? It makes sense to me. 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] switch vs elseif -- switchtable
On Mon, Jan 12, 2009 at 4:00 PM, Daevid Vincent wrote: > On Mon, 2009-01-12 at 15:31 -0500, Eric Butera wrote: > >> On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak >> wrote: >> > I've googled, and found some confusing answers. >> > I've tried searching the history of the news group, and only found info on >> > switch or elseif seperately. :( >> > >> > Strictly from a performance stand point, not preference or anything else, >> > is >> > there a benefit of one over the other? >> > >> > >> > for($i=0;$i<3;$i++){ >> >switch($i){ >> >case 0: >> >header pg1 code >> >break; >> >case 1: >> >header pg2 code >> >break; >> >case 3: >> >header pg3 code >> >break; >> >}; >> > }; >> > >> > >> > or would that be better served using an if...elseif structure? >> > >> > Frank > > > >> This might be of interest in answering your question: >> >> http://www.suspekt.org/switchtable/ > > > How do I install this? The page and .tgz both give no instructions?! > > Are there any benchmarks to show speed comparisons? > > Also, is there any plans to have this patch/extension incorporated into > the real PHP trunk? > > Daevid. > http://daevid.com > Hi Daevid, Someone wrote a quick bench on this url: http://www.suspekt.org/2008/07/31/switch-table-extension/ As for all your other questions, you'll have to contact the author. I just thought it applied to this thread. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif -- switchtable
On Mon, 2009-01-12 at 15:31 -0500, Eric Butera wrote: > On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak > wrote: > > I've googled, and found some confusing answers. > > I've tried searching the history of the news group, and only found info on > > switch or elseif seperately. :( > > > > Strictly from a performance stand point, not preference or anything else, is > > there a benefit of one over the other? > > > > > > for($i=0;$i<3;$i++){ > >switch($i){ > >case 0: > >header pg1 code > >break; > >case 1: > >header pg2 code > >break; > >case 3: > >header pg3 code > >break; > >}; > > }; > > > > > > or would that be better served using an if...elseif structure? > > > > Frank > This might be of interest in answering your question: > > http://www.suspekt.org/switchtable/ How do I install this? The page and .tgz both give no instructions?! Are there any benchmarks to show speed comparisons? Also, is there any plans to have this patch/extension incorporated into the real PHP trunk? Daevid. http://daevid.com
Re: [PHP] switch vs elseif
On Mon, Jan 12, 2009 at 3:58 PM, Frank Stanovcak wrote: > > ""Eric Butera"" wrote in message > news:6a8639eb0901121231r253eed48xe1974d8ef44ab...@mail.gmail.com... >> On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak >> wrote: >>> I've googled, and found some confusing answers. >>> I've tried searching the history of the news group, and only found info >>> on >>> switch or elseif seperately. :( >>> >>> Strictly from a performance stand point, not preference or anything else, >>> is >>> there a benefit of one over the other? >>> >>> >>> for($i=0;$i<3;$i++){ >>>switch($i){ >>>case 0: >>>header pg1 code >>>break; >>>case 1: >>>header pg2 code >>>break; >>>case 3: >>>header pg3 code >>>break; >>>}; >>> }; >>> >>> >>> or would that be better served using an if...elseif structure? >>> >>> Frank >>> >>> >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> >> >> This might be of interest in answering your question: >> >> http://www.suspekt.org/switchtable/ > > Wow...so if I read that right. the only difference in the root code of PHP > is that the pre compiled code is easier to read. PHP actually generates the > If...elseif...elseif... structure any way when it compiles the script. > > ewww. > > Frank. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Yea I was bummed out to hear that too because I had heard switch was faster. Oh well. :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
""Eric Butera"" wrote in message news:6a8639eb0901121231r253eed48xe1974d8ef44ab...@mail.gmail.com... > On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak > wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info >> on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, >> is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >>switch($i){ >>case 0: >>header pg1 code >>break; >>case 1: >>header pg2 code >>break; >>case 3: >>header pg3 code >>break; >>}; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > This might be of interest in answering your question: > > http://www.suspekt.org/switchtable/ Wow...so if I read that right. the only difference in the root code of PHP is that the pre compiled code is easier to read. PHP actually generates the If...elseif...elseif... structure any way when it compiles the script. ewww. Frank. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
"Ashley Sheridan" wrote in message news:1231793310.3558.55.ca...@localhost.localdomain... > On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: >> I've googled, and found some confusing answers. >> I've tried searching the history of the news group, and only found info >> on >> switch or elseif seperately. :( >> >> Strictly from a performance stand point, not preference or anything else, >> is >> there a benefit of one over the other? >> >> >> for($i=0;$i<3;$i++){ >> switch($i){ >> case 0: >> header pg1 code >> break; >> case 1: >> header pg2 code >> break; >> case 3: >> header pg3 code >> break; >> }; >> }; >> >> >> or would that be better served using an if...elseif structure? >> >> Frank >> >> >> > And a switch is a lot neater for dealing with these sorts of things. I > tend never to use if...elseif's at all, and use switches. Like Rob said, > you can fall into further cases below, and it's very simple to add more > at a later date. There is one place where an if...elseif would work and > a switch could not, and that is where you were performing lots of > different logic tests on different variables. Aside from that, I think > any speed benefit one would have over the other would be marginal. > > > Ash > www.ashleysheridan.co.uk > Yeah, I knew about the fall through benefit. :) I was just worried about speed since I have to loop through this several times and generate a pdf from it. Thanks folks! Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > And a switch is a lot neater for dealing with these sorts of things. I tend never to use if...elseif's at all, and use switches. Like Rob said, you can fall into further cases below, and it's very simple to add more at a later date. There is one place where an if...elseif would work and a switch could not, and that is where you were performing lots of different logic tests on different variables. Aside from that, I think any speed benefit one would have over the other would be marginal. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ > switch($i){ > case 0: > header pg1 code > break; > case 1: > header pg2 code > break; > case 3: > header pg3 code > break; > }; > }; > > > or would that be better served using an if...elseif structure? In some caes you can use a switch statement to avoid redundant code by allowing a particular case to contain code for one condition then allowing fall through to the next condition's code. The following is a lame example: In the above exmaple case 2 runs the code within it's block AND the code within case 3's block. Using else you would probably do one of the following: Or with code redundancy: One has to wonder about the readability of the case version though since one may not notice immediately the missing break statement. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch vs elseif
On Mon, Jan 12, 2009 at 3:15 PM, Frank Stanovcak wrote: > I've googled, and found some confusing answers. > I've tried searching the history of the news group, and only found info on > switch or elseif seperately. :( > > Strictly from a performance stand point, not preference or anything else, is > there a benefit of one over the other? > > > for($i=0;$i<3;$i++){ >switch($i){ >case 0: >header pg1 code >break; >case 1: >header pg2 code >break; >case 3: >header pg3 code >break; >}; > }; > > > or would that be better served using an if...elseif structure? > > Frank > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > This might be of interest in answering your question: http://www.suspekt.org/switchtable/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php