Re: IF or SWITCH

From: Date: Thu, 06 Apr 2006 06:26:18 +0000
Subject: Re: IF or SWITCH
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-233331@lists.php.net to get a copy of this message
Chris wrote:
Ray Hauge wrote:
Hello World! wait, not coding... (sorry, long night)
...
if ($count_lenders > 1)....
I would take it even further and setup a load of variables that hold the boolean states that you what to check in the 50 odd if/else statements... something like:
$ffelpEQtotal    = ($numFFELP == $numTotal);
$ffelptotEQtotal = (($enumFFELP + $numFFELP) == $numTotal);
$countlenders    = count($FFELP_Lenders);
$lendersGTone    = $countlenders > 1;
I would do this purely for readability in the if/else statements (i.e. so I could keep them as short as possible.) this is assuming the boolean states you are checking are quite repetitive (although the combination changes each time) and I would personally probably write it up as a switch statement because I feel it looks 'nicer' and it's more compact, thats down to personal opinion of course. something like: switch (true) { case $ffelpEQtotal && $lendersGTone: return array(true, 'A'); case $ffelptotEQtotal: return array(true, 'A'); // weeeeeee! etc :-) } <disclaimer> some people consider this use of switch to be a complete misuse - be careful with things that autocast cast to true - know what you're doing :-) </disclaimer>

« previous php.general (#233331) next »