-2

i want to make a multiple choice. the score will be in percentage, but i have conditionals. if the score = 100% then in the end will be displayed "C2 Proficiency". but if run and if i get score more than 30% then displayed "A1 Beginner". the script are not working. please help

here is the code:

<?php
if ($persentaje == 100){
    $hasil="C2 Proficiency";
}
if ($persentaje > 80) {
    $hasil="C1 Advanced";
}
if ($persentaje > 60) {
    $hasil="B2 Upper Intermediate";
} 
if ($persentaje > 50) {
    $hasil="B1 Intermediate";
} 
if ($persentaje > 40) {
    $hasil="A2 Pre Intermediate";
} 
if ($persentaje > 30) {
    $hasil="A2 Elementary";
} 
else {
    $hasil="A1 Beginner";
}    
?>
ssnake
  • 365
  • 2
  • 14
fflime
  • 25
  • 1
  • 1
  • 6

9 Answers9

3

Consider these two conditionals:

if ($persentaje == 100){
    $hasil="C2 Proficiency";
}
if ($persentaje > 80) {
    $hasil="C1 Advanced";
}

What happens when $persentaje == 100? The result will be "C1 Advanced". Because, well, 100 is also greater than 80. (And greater than 60, and greater than 50, etc.) So all of your conditions are true, thus each one is overwriting the previous one's $hasil value.

If you want the conditionals to be mutually exclusive, you probably want elseif:

if ($persentaje == 100){
    $hasil="C2 Proficiency";
}
elseif ($persentaje > 80) {
    $hasil="C1 Advanced";
}

Repeat for your other conditions until you have the final else.

David
  • 208,112
  • 36
  • 198
  • 279
1

All the if statements should be else if except for the first one. Otherwise you're just executing them one after the other

1

You maybe should use switch/case statement instead of if and if you don't want to change your if statement just use else if but it's kind different than switch/case

Sergi Case
  • 226
  • 2
  • 12
1

You could write your code like:

if ($persentaje == 100){
    $hasil="C2 Proficiency";
  } else if ($persentaje > 80) {
    $hasil="C1 Advanced";
  }else if ($persentaje > 60) {
    $hasil="B2 Upper Intermediate";
  }else if ($persentaje > 50) {
    $hasil="B1 Intermediate";
  }else if ($persentaje > 40) {
    $hasil="A2 Pre Intermediate";
  }else if ($persentaje > 30) {
    $hasil="A2 Elementary";
  } else {
    $hasil="A1 Beginner";
  } 

This would definitely work for you.

Code3d
  • 73
  • 5
1

Try reversing the if statements. The way you were using it, it would overwrite itself.

<?php
if ($persentaje > 30){
    $hasil="A2 Elementary";
  }if ($persentaje > 40) {
    $hasil="A2 Pre Intermediate";
  }if ($persentaje > 50) {
    $hasil="B1 Intermediate";
  } if ($persentaje > 60) {
    $hasil="B2 Upper Intermediate";
  } if ($persentaje > 80) {
    $hasil="C1 Advanced";
  } if ($persentaje == 100) {
    $hasil="C2 Proficiency";
  } if($persentaje < 30) {
    $hasil="A1 Beginner";
  } 
R.75
  • 59
  • 9
0

Working this in php is quite simple... Here is an example:

$money = 100;

    if($money) {
        echo "You have - ".$money;
    }else if($money < 100) {
        echo "You have less then needed";
    }else if ($money > 10) {
        echo "Your funds are critical";
    }else {
        echo "You don't have money!";
    }

Creating multiple if statements will cause them to execute one by another... Working with else if statement it will only execute the one that matches the required parameter...

The Bumpaster
  • 944
  • 7
  • 20
0

If i may suggest another option to use in PHP is the switch case functionality:

 switch(true) {
        case $percentaje == 100 :
            $hasil = "C2 Proficiency";
            break;
        case  $percentaje > 80:
            $hasil = "C1 Advanced";
            break;
    }

A) Looks neat in case of more options. B) It will choose a option based on what is valid

Reference : Using comparison operators in PHP switch

Community
  • 1
  • 1
0

use switch case

<?php
 switch($percentaje) {
    case $percentaje == 100 :
        $hasil = "C2 Proficiency";
        break;
    case  $percentaje > 80:
        $hasil = "C1 Advanced";
        break;
 case  $persentaje > 60:
        $hasil = "C1 Advanced";
        break;
 case  $persentaje > 50:
        $hasil = "C1 Advanced";
        break;
 case  $persentaje > 40:
        $hasil = "C1 Advanced";
        break;
 case  $persentaje > 30:
        $hasil = "C1 Advanced";
        break;

 default:
       $hasil="A1 Beginner";

}

 ?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
-1

You can use switch case and solve this problem.

Er Amit Anand
  • 50
  • 1
  • 7