如果是PHP中的/ Elseif语句

时间:2012-05-06 02:59:33

标签: php html if-statement

我们正在尝试为变量$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!";

            } 

6 个答案:

答案 0 :(得分:8)

当您打算使用比较=时,您正在使用作业==

作业评估到右侧的内容,所以

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
}

答案 1 :(得分:6)

您正在分配(=)。您需要逻辑上的平等(==)。

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

结帐comparisonlogical operators。还有switch() statement

答案 2 :(得分: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非常聪明,可以提供有关缺少条目的提示。如果输入值已经被约束/断言,则无论如何都不需要。)

答案 3 :(得分:2)

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

同样尝试这种查找表方法,在这种情况下更干净:

$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];

答案 4 :(得分: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!";

        } 

答案 5 :(得分:1)

你首先想到的是正确的,你需要两个等号。