in reply to Re: condensing code
in thread condensing code
If you invert the order of the checks, you can simplify to the following.
$test_nums = $total_nums > 180 ? $test_nums : $total_nums > 150 ? 6 : $total_nums > 120 ? 5 : $total_nums > 90 ? 4 : $total_nums > 60 ? 3 : $total_nums > 30 ? 2 : 1;
Since the breakpoints form continuous serious of multiples of 30, the above could be simplified further using a loop. Or better yet, using division.
if ( $total_nums <= 0 ) { $test_nums = 1; } elsif ( $total_nums <= 180 ) { $test_nums = int( ( $total_nums - 1 ) / 30 ) + 1; }
Update: I got sidetracked while writting my reply, so other people have posted similar solutions already. Please excuse any duplication.
|
|---|