switch(false) {
case \'blogHitd\':
echo(\'ffffd\');
break;
case false:
echo(\'bbbb\');
break;
default:
echo \'alert
Switching "true" is only useful if you've got functions or variables in your "case" line
switch(true)
{
case is_array($array):
echo 'array';
break;
default:
echo 'something else';
break;
}
From the PHP documentation on Booleans:
When converting to boolean, the following values are considered FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- an object with zero member variables (PHP 4 only)
- the special type NULL (including unset variables
- SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
The last sentence of this quoted passage is the line of interest in your case.
PHP will typecast values for you, don't forget:
php > var_dump(true == 'bloghitd');
bool(true)
In this scenario the switch only runs the first valid case.
It is useful in the case that you have more than one possible answer but you want to run only the first one. For example:
switch(true){
case 1 == 2:
echo '1 == 2';
break;
case 2 == 2:
echo '2 == 2';
break;
case 3 == 3:
echo '3 == 3';
break;
case 4 == 1:
echo '4 == 1';
break;
}
The output: 2 == 2
Both the second and third cases are true, but we only get the second (which is the first TRUE).
Note that switch/case does loose comparision.
http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose