Forum Moderators: coopster
I want to discuss matter of whever to use switch or not.
Let's say we have a variable that can be either $var "true/ false/ both"
To check that we can use switch:
switch($var) {
case "true": $value = "It was true!"; break;
case "false": $value = "It wasn't true."; break;
case "both": $value = "It was almost true, but not quite"; break;
default : die("Sorry wrong choice. Try again.");
$values = array("true"=>"It was true!", "false"=>"It wasn't true.", "both"=>"It was almost true, but not quite");
if(!($value = $values[$var])) die("Sorry wrong choice. Try again.");
PS. I think it's a good alternative to using switch in this case certainly!
switch($var) {
case "true":
$value = "It was true!";
$another_value = "Yes";
break;
case "false":
$value = "It wasn't true.";
header("Location: $url");
exit;
break;
case "both":
$value = "It was almost true, but not quite";
print "You can't have it both ways!";
break;
default :
die("Sorry wrong choice. Try again.");
}