I want use of switch and case in php and codeigniter library, i try it as following code, But I not receive output. what do i do?
Demo: http://codepad.viper-7.com/Wq0Noj
function indicators() {
$CI = &get_instance();
$Year = '1355';
$Month = '03';
switch ($Year) {
case 1354:
$key=array('0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6');
$output = $key[$Month-1];
break;
case 1355:
$key=array('0.6','0.7','0.2','0.4','0.7','0.1','0.7','0.2','0.5','0.9','0.4','0.8');
$output = $key[$Month-1];
break;
echo $output; // The output should be: 0.7
}
}
I think your
echoneeds to be outside of theswitchas well… checking to verify.Yep, the echo needs to be outside. The type should actually be coerced when comparing.
OUTPUT
And for your example:
OUTPUT
Which is correct according to the code.
'03' - 1 == 2.$key[2] == '0.2'As pointed out in the comment below by @vstm, the docs state that the “switch/case does loose comparision.”