Abaij
Abaij

Reputation: 873

Switch return wrong value

I have a switch statement to show a grade based on the points students get.

students with points >= 80 get A
students with points >=70 get B
students with points >= 50 get C
students with points >= 30 get D
students with points >= 0 get E

The problem is when the point is 0, it returns A instead of E. Here is the switch statement.

$point = 0;
switch ($point) {
        case $point >= 80:
            echo 'A';
            break;
        case $point >= 70:
            echo 'B';
            break;
        case $point >= 50:
            return 'C';
            break;
        case $point >= 30:
            echo 'D';
            break;
        case $point >= 0:
            echo 'E';
            break;
        default:
            echo 'F';
            break;
    }

Upvotes: 5

Views: 517

Answers (5)

Sebastian Brosch
Sebastian Brosch

Reputation: 43604

You try to compare $point to a boolean value (like $point >= 80). It is matching the first case because $point = 0 is false and $point >= 80 is false too, so it is matching the first case and echoing A. If you want to use a comparison on the cases you have to use the following code:

$point = 0;

switch (true) {
    case $point >= 80:
        echo 'A';
        break;
    case $point >= 70:
        echo 'B';
        break;
    case $point >= 50:
        echo 'C';
        break;
    case $point >= 30:
        echo 'D';
        break;
    case $point >= 0:
        echo 'E';
        break;
    default:
        echo 'F';
        break;
}

demo: https://ideone.com/lyBUmA

Another solution using if and elseif instead of switch:

$point = 0;

if ($point >= 80) {
    echo 'A';
} elseif ($point >= 70) {
    echo 'B';
} elseif ($point >= 50) {
    echo 'C';
} elseif ($point >= 30) {
    echo 'D';
} elseif ($point >= 0) {
    echo 'E';
} else {
    echo 'F';
}

demo: https://ideone.com/FRlorS

Upvotes: 6

Todd
Todd

Reputation: 44

Switch/Case does not support >... Try if/elseif....

$point = 0;


if ($point >= 80)
    echo 'A';
elseif ($point >= 70)
    echo 'B';
elseif ($point >= 50)
    echo 'C';
elseif ($point >= 30)
    echo 'D';
elseif ($point >= 0)
    echo 'E';
else
    echo 'F';

Upvotes: 0

Sathish Kumar D
Sathish Kumar D

Reputation: 274

yes obviously this question is duplicate. instead of using

switch ($point)

you should use

switch (true)

and it works i checked

<?php
$point = 0;
switch (true) {
    case $point >= 80:
        echo 'A';
        break;
    case $point >= 70:
        echo 'B';
        break;
    case $point >= 50:
        return 'C';
        break;
    case $point >= 30:
        echo 'D';
        break;
    case $point >= 0:
        echo 'E';
        break;
    default:
        echo 'F';
        break;
}

for more details please check this question

Upvotes: 1

newman
newman

Reputation: 424

This will not work, you try to use boolean condition. Use if else instead of it.

Upvotes: 1

Bharti
Bharti

Reputation: 1

You can use 2 comparisons in case like :

case $point >= 80:
   echo 'A';
   break;
case ($point < 80 && $point >= 70):
   echo 'B';
   break;

and so on..

Upvotes: 0

Related Questions