I want switch case to check with ===
rather than ==
.
<?php
$favcolor = "1";
switch ($favcolor) {
case 1:
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "1":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
This gives me:
Your favorite color is red!
But I want it to say green. How is that possible?
From https://secure.php.net/manual/en/control-structures.switch.php
Note that switch/case does loose comparison.
You cannot achieve this using switch, consider using if - elseif - else in such case like:
if ( $favcolor === 1 )
echo "Your favorite color is red!";
elseif ( $favcolor === "blue" )
echo "Your favorite color is blue!";
elseif ( $favcolor === "1" )
echo "Your favorite color is green!";
else
echo "Your favorite color is neither red, blue, nor green!";