PHP:如果/ elseif / else语句不正确更好的方法?

时间:2013-05-01 05:07:35

标签: php

如何使这个if语句适用于0,1和2只猫?

当$ cats = 0或1时,它回声“没关系,你只有2只猫”

任何代码建议?

 <?php
        //If you have more then 2 cats it is illegal
        //if you have 20 or more cats "you are a freak!"
        //If you have 2 cats "It is fine you only have 2 cats".
        //If you have 1 cats "It is fine you only have 1 cat".
        //If you have 0 cats "You don't have any cats no problem".
        $cats = 25;
        if ($cats > 2 && $cats < 20){
            echo "You have too many cats, it is illegal!";
        } elseif ($cats >= 20) {
            echo "you are a freak!";
        } elseif ($cats = 2) {
            echo "It is fine you only have 2 cats";
        } elseif ($cats = 1) {
            echo "It is fine you only have 1 cat";
        } elseif ($cats = 0){
            echo "You don't have any cats no problem";
        }
    ?>

8 个答案:

答案 0 :(得分:2)

使用==进行比较,=用于分配。

此外,请考虑将代码重新排列为:

switch($cats) {
case 0:
    echo "You don't have any cats";
    break;
case 1:
    echo "You have 1 cat";
    break;
case 2:
    echo "You have 2 cats";
    break;
default:
    if( $cats < 20) echo "You have too many cats";
    else echo "You are a freak!";
}

答案 1 :(得分:1)

应该是==(用于比较)而不是=(用于分配)

} elseif ($cats == 2) {

答案 2 :(得分:1)

您使用了错误的比较运算符语法

<?php
            //If you have more then 2 cats it is illegal
            //if you have 20 or more cats "you are a freak!"
            //If you have 2 cats "It is fine you only have 2 cats".
            //If you have 1 cats "It is fine you only have 1 cat".
            //If you have 0 cats "You don't have any cats no problem".
            $cats = 25;
            if ($cats > 2 && $cats < 20){
                echo "You have too many cats, it is illegal!";
            } elseif ($cats >= 20) {
                echo "you are a freak!";
            } elseif ($cats == 2) {
                echo "It is fine you only have 2 cats";
            } elseif ($cats == 1) {
                echo "It is fine you only have 1 cat";
            } elseif ($cats == 0){
                echo "You don't have any cats no problem";
            }

答案 3 :(得分:1)

===之间存在差异,甚至===

检查逻辑比较图表 -

http://php.net/manual/en/language.operators.comparison.php

答案 4 :(得分:1)

尝试使用==代替=

else if ($cats == 2){}

答案 5 :(得分:0)

如果PHP中的语句需要== not =才能工作。

答案 6 :(得分:0)

比较运算符出错。只需使用它(我用当前的方式编码并进行一些修正):

 <?php
        //If you have more then 2 cats it is illegal
        //if you have 20 or more cats "you are a freak!"
        //If you have 2 cats "It is fine you only have 2 cats".
        //If you have 1 cats "It is fine you only have 1 cat".
        //If you have 0 cats "You don't have any cats no problem".
        $cats = 25;
    if ($cats == 0){
            echo "You don't have any cats no problem";
        }else if ($cats == 1) {
            echo "It is fine you only have 1 cat";
        }  else if ($cats == 2) {
            echo "It is fine you only have 2 cats";
        }  else if ($cats > 2 && $cats < 20){
            echo "You have too many cats, it is illegal!";
        } else {
            echo "you are a freak!";
        }
    ?>

答案 7 :(得分:0)

如果您只是将 = 作业)替换为 == 比较您的if语句中的>)运算符!但是,我会在这个特定情况下使用switch语句,在向下滚动代码时,它的可读性和可理解性更强;)

switch($cats) {
   case 0: echo "You don't have any cats"; break;
   case 1: echo "You have 1 cat"; break;
   case 2: echo "You have 2 cats"; break;
   default: echo ($cats < 20 ? "You have too many cats" : "You are a freak!");
}

Ternary Operator在这样的情况下非常方便!