-4

如何使这个 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";
        }
    ?>
4

8 回答 8

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!";
}
于 2013-05-01T05:09:52.123 回答
1

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

} elseif ($cats == 2) {
于 2013-05-01T05:09:10.900 回答
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";
            }
于 2013-05-01T05:09:12.713 回答
1

=== 甚至之间有区别===

检查逻辑比较图表 -

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

于 2013-05-01T05:09:20.027 回答
1

尝试使用==而不是=

else if ($cats == 2){}

于 2013-05-01T05:10:34.753 回答
0

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

于 2013-05-01T05:09:22.790 回答
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!";
        }
    ?>
于 2013-05-01T05:13:09.140 回答
0

如果您只是在语句中将= ( assignment )替换为== ( comparison ) 运算符,您的代码可能会起作用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!");
}

三元运算符在这种情况下也非常方便!

于 2013-05-01T05:13:44.013 回答