Re: switch vs elseif

From: Date: Mon, 12 Jan 2009 20:51:21 +0000
Subject: Re: switch vs elseif
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-286144@lists.php.net to get a copy of this message
"Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message news:1231793310.3558.55.camel@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

« previous php.general (#286144) next »