-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?– MarceloBoni
string(1) "0" is the q shows.
– Vitor Leite
the switch does not work as you imagine, think in your case a
if(condition) {} elseif(condition) {}
would be better even– MarceloBoni
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 }"
– MarceloBoni
But the switch did not come to replace a heap of if and elses ?
– Vitor Leite
Ah understood. The switch would be to compare an exact value and not a range between values.
– Vitor Leite
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 }
– MarceloBoni
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.
– Vitor Leite