PHP如果那时和switch语句不起作用

时间:2022-11-22 21:28:37

OK, first, this is very special problem. I was working on PHP for long time, and I don't know why this happened.

好的,首先,这是一个非常特殊的问题。我在PHP上工作了很长时间,我不知道为什么会这样。

I have a function adminUpdate. This function will return true. I set it always return true for testing.

我有一个函数adminUpdate。此函数将返回true。我设置它总是返回true进行测试。

Then I have function take that result.

然后我有功能取得结果。

static function result2JSON($result,$options = array()) {

        if($result == "permission") {
            echo "permission";
        }

        if($result == true) {
            echo "true";
        }


        switch ($result) {
            case 'permission':
                die($result."xxx permission");
                $json = self::setJSON("Permission");
                break;
            case 'exist':
                $json = self::setJSON("Exist");
                break;
            case false:
                $json = self::setJSON("Error");
                break;
            case "":
                $json = self::setJSON("Error");
                break;
            case 1 :
                $json = self::setJSON("OK");
                break;
            case true:
                $json = self::setJSON("OK");
                break;
            default:
                $json = self::setJSON("OK");
                break;
        }


        $json = array_merge($json,$options);


        return $json;

    }

These "Echo" use for testing on this case. So, $result always = true before taken by this function.

这些“Echo”用于测试此案例。所以,$ result总是= true才能被这个函数占用。

But this is output I got:

但这是我得到的输出:

permissiontrueResult = 1 IN permission section

That mean the $result = Permission , and then == true, and then == "permission" on switch. Why is that ?

这意味着$ result = Permission,然后== true,然后在开关上==“permission”。这是为什么 ?

3 个解决方案

#1


2  

You may want to use identity check === rather than a check for equality.

您可能希望使用身份检查===而不是检查是否相等。

In php, a non empty string is interpreted as true.

在php中,非空字符串被解释为true。

$result = "permission";
if($result)
     echo 'String interpreted as true';

Take a Look at Boolean page on php.net (Short) http://php.net/manual/en/language.types.boolean.php and the PHP Comparison Operators page... http://www.php.net/manual/en/language.operators.comparison.php

看看php.net上的布尔页面(简称)http://php.net/manual/en/language.types.boolean.php和PHP比较运算符页面... http://www.php.net /manual/en/language.operators.comparison.php

#2


1  

Strings with stuff in them evaluate to true if you try and treat them as a boolean. (well, okay, the string "0" evaluates to False. php is weird.)

如果您尝试将它们视为布尔值,则包含其中内容的字符串将评估为true。 (好吧,字符串“0”的计算结果为False.php很奇怪。)

So, if you want to see if a variable is actually true, you have to use a comparison that also checks type:

因此,如果要查看变量是否确实为真,则必须使用也检查类型的比较:

if ($result === "permission") { ... }

if ($result === True) { ... }

#3


0  

You should replace every occurrence of == with ===

您应该用===替换每次出现的==

#1


2  

You may want to use identity check === rather than a check for equality.

您可能希望使用身份检查===而不是检查是否相等。

In php, a non empty string is interpreted as true.

在php中,非空字符串被解释为true。

$result = "permission";
if($result)
     echo 'String interpreted as true';

Take a Look at Boolean page on php.net (Short) http://php.net/manual/en/language.types.boolean.php and the PHP Comparison Operators page... http://www.php.net/manual/en/language.operators.comparison.php

看看php.net上的布尔页面(简称)http://php.net/manual/en/language.types.boolean.php和PHP比较运算符页面... http://www.php.net /manual/en/language.operators.comparison.php

#2


1  

Strings with stuff in them evaluate to true if you try and treat them as a boolean. (well, okay, the string "0" evaluates to False. php is weird.)

如果您尝试将它们视为布尔值,则包含其中内容的字符串将评估为true。 (好吧,字符串“0”的计算结果为False.php很奇怪。)

So, if you want to see if a variable is actually true, you have to use a comparison that also checks type:

因此,如果要查看变量是否确实为真,则必须使用也检查类型的比较:

if ($result === "permission") { ... }

if ($result === True) { ... }

#3


0  

You should replace every occurrence of == with ===

您应该用===替换每次出现的==