将开关中的函数结果分配给变量


Assign result of function within switch to variable

这里有一些伪代码(写得不正确,我的点是变量,而不是开关):

switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}

这应该怎么写:

$variable = 情况 1 中的函数结果。

你的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)作为代码的一部分在案例/像这样中断块

$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.
 ?>

在我的示例中,我需要在变量中输入大小写的值。