Kevin Brown
Kevin Brown

Reputation: 12650

PHP: Foreach scoring help

I have this php that scores a test based on hourly answers:

    $score_a = 0;

    foreach(array(4,5,10) as $a){
        if ($a >= 2 && $a <= 4) {
            $score_a += 1;
        } else if ($a > 4 && $a <= 8) {
            $score_a += 3;
        } else if ($a > 8) {
            $score_a += 0;
        }
    };

I need the final "else if" to score a bit differently. Instead of adding .5 once if the value is >8, I need it to add .5 for each whole number above 8.

So this score needs to be 5 and not 4.5.

Upvotes: 1

Views: 112

Answers (1)

K Prime
K Prime

Reputation: 5849

You mean like: $score_a += floor($a - 8) * .5; ?

You can use it like so:

foreach(array(4,5,10) as $a){
    if ($a >= 2 && $a <= 4) {
        $score_a += 1;
    } else if ($a > 4 && $a <= 8) {
        $score_a += 3;
    } else if ($a > 8) {
        $score_a += floor($a - 8) * .5;
    }
};

Upvotes: 2

Related Questions