【问题标题】:If/Elseif statement in PHPPHP 中的 if/Elseif 语句
【发布时间】:2012-05-06 02:59:33
【问题描述】:

我们正在尝试为变量 $day 创建 if/elseif 语句。我们在与此 PHP 关联的 HTML 代码中定义了变量及其值。我会将 PHP 部分粘贴到此页面。问题是结果页面只给出“$day = 1”的响应。有谁知道此代码中的错误会导致这种情况发生?我们也尝试使用两个等号,但结果更糟!

echo "<p>";
                if ($day = 1) {
                    echo "Sunday Funday! What COMMITMENT to going out!";
                } elseif ($day = 2) {
                    echo "The start of what's sure to be a rough week. Drink away your sorrows.";
                } elseif ($day = 3) {
                    echo "Epic night! SO many themes at the bars!";
                } elseif ($day = 4) {
                    echo "Hump day!! But seriously, what are you doing...? Aren't you too hungover from last night?";
                } elseif ($day = 5) {
                    echo "Thirsty Thursday!! It's close enough to the weekend... right?";
                } elseif ($day = 6) {
                    echo "It's Friiiiiiday, Friiiiiiiday, Gotta get down on Friiiiiiiiiday!";
                } elseif ($day = 7) {
                    echo "It's FRATurday! Go have some fun!";

            } 

【问题讨论】:

  • 你肯定需要 2 个等号,因为 = 是赋值
  • 您想专门使用 if/else 吗?这种情况下最好使用switch,
  • 您确实需要使用==。 “让情况变得更糟”是什么意思?
  • 看来你需要花更少的时间在酒吧里,花更多的时间在你的代码上;)
  • 第一个= 是赋值运算符,== 是要在if 中使用的条件运算符。同样对于您的逻辑switch/case 方法我会更合适。

标签: php html if-statement


【解决方案1】:

当您打算使用比较 == 时,您正在使用赋值 =

Assignment 计算在右侧,所以

if ($day = 1)

一样
if (1)

由于these rules

if (true)

这应该可以解释为什么程序的行为如此,当然您现在知道如何修复它了。但如果你使用switch 声明会更好:

switch($day) {
    case 1:
        echo "Sunday Funday! What COMMITMENT to going out!";
        break;
    case 2:
        echo "The start of what's sure to be a rough week.";
        break;
    // etc etc
}

【讨论】:

  • 当我们添加两个等号时,整个echo语句从结果页面中消失了。
  • 那么问题显然不是比较,而是$day 不等于您正在测试的任何值。您可以使用var_dump($day) 来查看它实际上是什么。
【解决方案2】:

您正在分配 (=)。您需要一个逻辑相等 (==)。

if ($day == 1) {
  echo "Sunday Funday! What COMMITMENT to going out!";
}

查看comparisonlogical operators。还有switch() statement

【讨论】:

    【解决方案3】:

    如果要检查的条件列表很长,则通常不希望编写一长串 if / else 语句。而是尝试使用switch 块,甚至是array 映射:

     $map = array(
        1 => "Sunday Funday! What COMMITMENT to going out!",
        2 => "The start of what's sure to be a rough week. Drink away your sorrows.",
        3 => "Epic night! SO many themes at the bars!",
        4 => "...",
     );
    

    这样代码就变得非常简单了:

     echo $map[ $day ];
    

    (理想情况下会使用isset 检查。但在开发阶段,PHP 足够聪明,可以提示缺少条目。如果输入值已经被约束/断言,则无论如何都不需要。)

    【讨论】:

      【解决方案4】:

      你想让所有的条件都像这样

                  if ($day == 1) {
                      echo "Sunday Funday! What COMMITMENT to going out!";
                  } elseif ($day == 2) {
                      echo "The start of what's sure to be a rough week. Drink away your sorrows.";
                  } elseif ($day == 3) {
                      echo "Epic night! SO many themes at the bars!";
                  } elseif ($day == 4) {
                      echo "Hump day!! But seriously, what are you doing...? Aren't you too hungover from last night?";
                  } elseif ($day == 5) {
                      echo "Thirsty Thursday!! It's close enough to the weekend... right?";
                  } elseif ($day == 6) {
                      echo "It's Friiiiiiday, Friiiiiiiday, Gotta get down on Friiiiiiiiiday!";
                  } elseif ($day == 7) {
                      echo "It's FRATurday! Go have some fun!";
      
              } 
      

      【讨论】:

        【解决方案5】:

        比较运算符是== 而不是=

        还可以尝试这种查找表方法,在这种情况下更简洁:

        $day_msg = array(
            1 => "Sunday Funday! What COMMITMENT to going out!",
            2 => "The start of what's sure to be a rough week. Drink away your sorrows.",
            3 => "Epic night! SO many themes at the bars!",
            4 => "Hump day!! But seriously, what are you doing...? Aren't you too hungover from last night?",
            5 => "Thirsty Thursday!! It's close enough to the weekend... right?"
            6 => "It's Friiiiiiday, Friiiiiiiday, Gotta get down on Friiiiiiiiiday!",
            7 => "It's FRATurday! Go have some fun!"
        );
        echo "<p>";
        echo $day_msg[$day];
        

        【讨论】:

          【解决方案6】:

          您首先认为是对的,您需要两个等号。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-01
            相关资源
            最近更新 更多