Answer the question
In order to leave comments, you need to log in
Who faced a strange bug in PHP when using switch case?
<?php
$userLang = 0;
switch($userLang) {
case ($userLang>2):
$userLang = 1;
break;
}
echo $userLang;
Answer the question
In order to leave comments, you need to log in
Because you mixed up the place of the condition and the place of the value of the condition.
Why does it work with 0 like this:
//В switch пишется условие, результат выполнения которого ищется в case
switch(0) {
case (0>2)://false, итог: 0==false, ответа да, case сработает
$userLang = 1;
break;
}
//В switch пишется условие, результат выполнения которого ищется в case
switch(1) {
case (1>2)://false, итог: 1==false, ответа нет, case не сработает
$userLang = 1;
break;
}
//В switch пишется условие, результат выполнения которого ищется в case
switch(3) {
case (3>2)://true, итог: 3==true, ответа да, case сработает
$userLang = 1;
break;
}
what you wrote is the same
<?php
$userLang = 0;
switch(false) {
case false:
$userLang = 1;
break;
}
echo $userLang;
You have a strange value in case. I don't think you really understand how switch works.
To compare with $userLang , the expression ($userLang>2) is implicitly cast to int, resulting in 0.
This is not a bug in PHP.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question