Is that a bug in the PHP SWITCH?

Asked

Viewed 390 times

-1

I’m trying to make a switch in PHP, but it returns me some very strange values.

For example, if the variable $xp is equal to 0, it returns to me that the level is 20.

I don’t get it '-'

switch($xp){
        case($xp <= 60);
        $nivel = 0;
        break;

        case($xp <= 200);
        $nivel = 1;
        break;

        case($xp <= 350);
        $nivel = 2;
        break;

    //... vários case dps.

        case($xp <= 4375);
        $nivel = 18;
        break;

        case($xp <= 5000);
        $nivel = 19;
        break;

// se $xp for igual a 0 ele me retorna $nivel = 20
        case($xp > 5000);
        $nivel = 20;
        break;
    }

The $xp is returned from the bank, is a INT.

  • before the switch case, if you give a echo var_dump($xp) shows what?

  • string(1) "0" is the q shows.

  • the switch does not work as you imagine, think in your case a if(condition) {} elseif(condition) {} would be better even

  • In the case of the switch, read this to better understand how it works: "if x is { equal to 60, then such } { equal to 80, then such }{ equal to 200, then such }"

  • But the switch did not come to replace a heap of if and elses ?

  • Ah understood. The switch would be to compare an exact value and not a range between values.

  • You can compare values, but I don’t think that would be the best approach, you could do so, for example: if x equals 60, equals 61, equals 62, .... is equal to 80 { do that } is equal to 81, is equal to 82, .... is equal to 100 { do that }

  • As I need to compare high values, it will be an eternity. Well I’ll try with if, since there is no change in logic, only in the same structure.

Show 3 more comments

2 answers

9


There is no bug in the switch. The problem lies in the logic of your code. Because, for all cases, it is returning true, since 0 is less than any of the values of its cases, with the exception of $xp > 5000. Soon, it is returning the last case assignment. If you remove the case($xp > 5000), will see that the value of $nivel will be 19.

In your case, the ideal, however less intuitive it may seem, is to use if and else if, due to the range you are using. The switch is used in case of cases exact, such as the switch of a lamp, the selector of a washing machine, etc.

You could even use the switch structure, but your code would probably be too big and unviable, it would be something like this:

switch($xp){
    case(60): case(61): case(62): case(...): case(199):
        $nivel = 0;
        break;
    case(200): case(201): case(202): case(...): case(499):
        $nivel = x;
        break;
    ....
    default:
        break;

With else if:

if($xp > 0 && $xp < 200){
    $nivel = 0;
}else if($xp > 200 && $xp < 500){
    $nivel = x;
}

0

It has already been explained the reason for the mistake, and that what you want is not with switch, but that doesn’t mean a lot of if else be the best option

An example with array and loop:

//Crie um array em que as chaves são
//os níveis e os valores serão o que
//irá usar para comparar
$array = [
  0 => [0, 60],
  1 => [60, 200],
  2 => [200, 350]
  //...
];

//Por padrão o nível vai ser 20, seria como um else
$nivel = 20;

//Percorre o array e verifica o valor da
//variável $xp com as chaves
foreach ($array as $chave => $valor) {
  //Se o $xp for > ou = ao primeiro numero do $valor
  //e for menor que o segundo numero do $valor
  if ($xp >= $valor[0] && $xp < $valor[1]) {
    //A variavel $nivel recebe a chave
    $nivel = $chave;
  }
}

Then you will only have one condition (which will be executed once for each item within the loop), if you need to add a new level or change the old one, just update the array

In that case the keys to $array would not need to be defined explicitly, but I left for better understanding

Browser other questions tagged

You are not signed in. Login or sign up in order to post.