1

我们正在尝试为变量创建 if/elseif 语句$day。我们在与此 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!";

            } 
4

6 回答 6

8

=当您打算使用比较时,您正在使用分配==

赋值计算右侧的内容,所以

if ($day = 1)

是相同的

if (1)

由于这些规则,这与

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
}
于 2012-05-06T03:02:41.827 回答
6

如果您有一长串要检查的条件,您通常不想编写一长串 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 足够聪明,可以提示缺少条目。如果输入值已经受到约束/断言,则无论如何都不需要。)

于 2012-05-06T03:08:43.520 回答
6

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

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

查看比较逻辑运算符。也是switch()声明

于 2012-05-06T03:02:32.660 回答
2

比较运算符 is==和 not =

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

$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];
于 2012-05-06T03:03:04.583 回答
2

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

            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!";

        } 
于 2012-05-06T03:04:20.610 回答
1

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

于 2012-05-06T03:03:38.503 回答