在switch语句中更新PHP变量(简单)

时间:2014-08-29 20:25:51

标签: php variables scope switch-statement

在PHP中,我有一个名为$ chickens的开关之外的值,等于1.在通过switch语句之后,我希望将值更改为b。

//original value of var
$chickens = 1;

switch ($chickens) {
  case "0":
    $chickens === "a";
    break;
  case "1":
    $chickens === "b";
    break;
  case "2":
    $chickens === "c";
    break;
  case "3":
    $chickens === "b";
    break;
}

//want this to be "b" defined in switch
echo $chickens;

由于某种原因,该值不会更新:(。任何想法

1 个答案:

答案 0 :(得分:6)

您使用了错误的操作员。 =是赋值运算符。 ===是比较运算符:

//original value of var
$chickens = 1;

switch ($chickens) {
  case "0":
    $chickens = "a";
    break;
  case "1":
    $chickens = "b";
    break;
  case "2":
    $chickens = "c";
    break;
case "3":
    $chickens = "d";
    break;
}

//want this to be "b" defined in switch
echo $chickens;