RE: [PHP] IF or SWITCH
| From: | ray dot hauge at americanstudentloan dot com | Date: | Thu, 06 Apr 2006 05:32:03 +0000 |
| Subject: | RE: [PHP] IF or SWITCH | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-233328@lists.php.net to get a copy of this message | ||
> -------- Original Message --------
> Subject: Re: [PHP] IF or SWITCH
> From: Chris <dmagick@gmail.com>
> Date: Wed, April 05, 2006 8:32 pm
> To: Ray Hauge <ray.hauge@americanstudentloan.com>
> Cc: PHP-General <php-general@lists.php.net>
>
> Ray Hauge wrote:
> > Hello World! wait, not coding... (sorry, long night)
> >
> > Okay, I finally finished hashing out all the logic for a very complex set of
> > rules to determine what "type" an application should be set to. I won't
> > bore
> > you with the details of it, but the question is...
> >
> > I have 57 if/elseif/else statements because of all the different criteria. Is
> > it considered better programming practice to use if/elseif/else statements
> > over a switch(true) case (true && false || true || false) syntax?
> >
> > Basically, I'm not too happy with the readability of the code, but I'm afraid
> > that at this point there's not much I can do...
> >
> > code snippet:
> >
> > if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP ==
> > $numTotal){
> > $retVal = array(TRUE, 'A');
> > }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders)
> > > 1 &&
> > $enumFFELP + $numFFELP == $numTotal){
> > $retVal = array(TRUE, 'A');
> > }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) >
> > 1 &&
> > $numFFELP + $numCONS == $numTotal){
> > etc.
>
> Are you in a function? Maybe it'll be clearer/easier to follow if you
> return when you find the right condition:
>
> if ($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal)
> {
> return array(TRUE, 'A');
> }
>
> if (.....
>
> PS - count($array) does a count every time, so depending on how large
> you expect this array to get, it could be quicker (processing time) to:
>
> $count_lenders = count($FFELP_Lenders);
>
> if ($count_lenders > 1)....
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
I should change that count out. Thanks for the tip. This is actually
in a function, but I prefer to only have one location of returning. I
think it makes it easier to debug.
Ray