【发布时间】:2013-11-12 14:33:29
【问题描述】:
这里有一些伪代码(写的不正确,我的 ? 点是变量,不是开关):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
这个应该怎么写:
$variable = 案例 1 中的函数结果。
【问题讨论】:
标签: php switch-statement
这里有一些伪代码(写的不正确,我的 ? 点是变量,不是开关):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
这个应该怎么写:
$variable = 案例 1 中的函数结果。
【问题讨论】:
标签: php switch-statement
您的 switch 陈述是错误的。它要求每个案例之间有一个break关键字
$action = 1;
$result = "Success";
switch ($action) {
case 1:
$variable = $result;
echo $variable;//prints Success
//this is a function
break; // like this
case 2:
//this is a function
break;//
//etc.
}
【讨论】:
只需运行函数(您也可以传入 args)作为 case / break 块中代码的一部分,如下所示:
$action = 1;
switch ($action) {
case 1:
$variable = someFunctionOne();
break;
case 2:
$variable = someOtherFunctionTwo();
break;
//etc.
}
【讨论】:
如何改变php切换的结果。 例如:
<?php
//variables of cases
$var_1 = 1;
$var_2 = 2;
$var_3 = 3;
$var_0 = 0;
//end variables of cases
//action variable
$action = 10;
//end action variable
//start switch
switch ($action) {
case "1":
echo "$var_1;";
break;
case "2":
echo "$var_2;";
break;
case "3":
echo "$var_3;";
break;
default:
echo "$var_0;";
}
//receives the value of the switch.
$switch_result = get_result_case;
//in this my example I need to enter the value of the case in a variable.
?>
在我的示例中,我需要在变量中输入 case 的值。
【讨论】: