0

ここにいくつかの疑似コードがあります(正しく書かれていません。私の ? のポイントは変数であり、スイッチではありません):

switch ($action) {

case "1":
//this is a function

case "2":
//this is a function

//etc.
}

これはどのように書かれるべきですか:

$variable = ケース 1 の関数の結果。

4

3 に答える 3

0

あなたの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.
    }
于 2013-11-01T11:37:21.240 に答える
0

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

この例では、ケースの値を変数に入力する必要があります。

于 2021-07-13T00:37:35.997 に答える