The case
It turns out that doing some tests I have found something unusual behavior or that I had not experienced before.
The issue is that the switch function allows comparisons to be made consecutively of the type:
//...
switch ($i) {
case 0:
case 1:
case 'a':
echo "...";
break;
//...
Similar examples appear in the php manual .
The theory is that if the value of $i is 0 , 1 , or a should activate the echo .
In the case I show:
- The values
0orawould return0. - The values
1orbwould return1. - The values
2orcwould return2.
But I do not know what motive causes the expected results with the following example:
class Test {
const A = 0;
const B = 1;
const C = 2;
public function select($value)
{
switch($value) {
case 'a':
case self::A:
return self::A;
break;
case 'b':
case self::B:
return self::B;
break;
case 'c':
case self::C:
return self::C;
break;
default:
return 3;
}
}
}
$test = new Test();
echo $test->select('b');
A functional example in a Sandbox .
The problem
All integer return their correct value, but strings always return
0.
The question
Why is it that the values with string do not return the value that corresponds to it?