简体   繁体   English

PHP中的If / Elseif语句

[英]If/Elseif statement in PHP

We are trying to create an if/elseif statement for the variable $day . 我们正在尝试为变量$day创建一个if / elseif语句。 We defined the variable and its values in the HTML code associated with this PHP. 我们在与此PHP关联的HTML代码中定义了变量及其值。 I will paste the PHP section to this page. 我将PHP部分粘贴到此页面。 The problem is that the results page is only giving the response for "$day = 1". 问题在于结果页面仅给出“ $ day = 1”的响应。 Does anyone know of an error in this code that would cause this to happen? 有谁知道此代码中的错误会导致这种情况发生? We also tried using two equals signs but that made it worse! 我们还尝试使用两个等号,但情况更糟!

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

            } 

You are using assignment = when you mean to use comparison == . 当您打算使用比较==时,您正在使用分配=

Assignment evaluates to what is at the right hand side, so 作业的评估结果在右侧,因此

if ($day = 1)

is the same as 是相同的

if (1)

which due to these rules is the same as 由于这些规则而与

if (true)

This should explain why the program behaves like it does and of course you now know how to fix it. 这应该可以解释程序为什么会表现得如此,当然您现在知道如何修复它了。 But it would be even better if you used a switch statement: 但是,如果使用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
}

Your are assigning ( = ). 您正在分配( = )。 You need a logical equality ( == ). 您需要逻辑上的相等( == )。

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

Check out comparison and logical operators . 查看比较逻辑运算符 Also the switch() statement . 还有switch()语句

If you have a long list of conditions to check against, you do not typically want to write a long chain of if / else statements. 如果要检查的条件列表很长,通常就不需要编写一长串的if / else语句。 Rather try a switch block, or even an array map: 而不是尝试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 => "...",
 );

As then the code becomes pretty simple: 然后,代码变得非常简单:

 echo $map[ $day ];

(Ideally would use an isset check. But for the development stage PHP is clever enough to give hints about missing entries. If the input values are already constrained/asserted, no need anyway.) (理想情况下,将使用isset检查。但是在开发阶段,PHP足够聪明,可以提供有关缺少条目的提示。如果输入值已经受到约束/确定,则无论如何都不需要。)

The comparison operator is == and not = . 比较运算符是==而不是=

Also try this lookup table approach, which is cleaner in this situation: 还可以尝试使用这种查找表方法,这种方法在这种情况下更干净:

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

you want to make all the conditions like this 您想要使所有这样的条件

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

        } 

您首先想到的是对的,您需要两个相等的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM