所以我有一个名为multiplier的变量,它包含一个特定的值,具体取决于用户注册的内容。我想在这里写的是
如果乘数等于"久坐"然后给它$sedmultiplier
如果乘数等于"轻微"然后给它$lightmultiplier
我坚持这一点,似乎无法弄清楚这是怎么写的。
switch ($multiplier==) {
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
multiplier==0;
}
答案 0 :(得分:2)
将 $ multiplier == 替换为 $ multiplier ,您的代码应该有效。
像这样:
switch ($multiplier)
{
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
$multiplier==0;
}
答案 1 :(得分:2)
就像@jeroen建议的那样,我会将乘数设置为数组。这样您就可以重复使用它们并更轻松地添加/删除乘数。
// Create the array with name => multiplier
$multipliers = [
"sedentary" => $sedmultiplier,
"lightly" => $lightmultiplier,
"moderately" => $modmultiplier,
"very" => $verymultiplier,
"extremely" => $extrememultiplier,
// ... just add more here, if needed...
];
// Check if we have a key with the current name.
// If we do, return the value, otherwise return 0 as default.
$multiplier = array_key_exists($multiplier, $multipliers)
? $multipliers[$multiplier]
: 0;