0

我在工作中遇到了这个问题,想知道为什么 PHP 会这样:

$test = "insert";

$isInsert1 = $test == "update";             // false
$isInsert2 = (boolean) ($test == "update"); // false
$isInsert3 = (boolean) $test == "update";   // true

$isInsert3 应该像其他两个变量一样返回 false,不是吗?我认为出于某种原因,我不知道,php 在将 $test 变量与“更新”字符串进行比较之前会考虑它。

我希望有人向我解释这种行为。

4

5 回答 5

5

在第三行中,(boolean) $test == "update"被解释为((boolean) $test) == "update"。然后,PHP 尝试评估true == "update",因为一个非空字符串为真,然后,右侧,"update"转换为真,所以true == true为真。

于 2013-10-21T19:27:43.357 回答
2

PHP在这里“看到”的是什么:

第一的:

$isInsert1 = $test == "update";             // false

<=> $isInsert1 = ($test == "update"); 
<=> $isInerst1 = ("insert" == "update");
<=> $isInerst1 = (false) // -> false.

第二:

$isInsert2 = (boolean) ($test == "update"); // false

<=> $isInerst2 = (boolean) ("insert" == "update");
<=> $isInerst2 = (boolean) (false);
<=> $isInsert2 = false; // false

第三:

$isInsert3 = (boolean) $test == "update";   // true

<=> $isInsert3 = (((boolean) $test) == "update"); //$test "isset"
<=> $isInsert3 = (true == "update"); //"update" "isset"  ps.: true === "update" would be false!
<=> $isInsert3 = (true); // -> true

见: http: //php.net/manual/en/language.operators.precedence.php

我认为您的主要“困惑”是事实,即true == "update"等于true. 这是因为==在 PHP 中意味着相等,但===意味着相同!

有关更多信息,请参见此处:http ://www.php.net/manual/en/language.operators.comparison.php

bool == ANYTHING将导致右侧被强制转换为布尔值。当转换为布尔时:

(boolean)1 == (boolean)2 == (boolean)"AnyNotEmptyString" == true

false == (boolean)0 == (boolean)null == (boolean)"". (以及我错过的任何东西)

注意:即使是布尔值的字符串表示也不会被转换为有问题的布尔值。将字符串与布尔值进行比较时,重要的是:字符串是否为空(或 null,或“0”)?那么它是错误的!

(Boolean)"false" == false // -> will return false
(Boolean)"false" == true // -> will return true.
(Boolean)"true" == true // -> will return true.
(Boolean)"true" == false// -> will return false.
(Boolean)"0" == true // -> will return false.
(Boolean)"0" == false// -> will return true.

剪断:

   <?php
echo ((Boolean)"false" == false)?"true":"false";
echo "<br />";
echo ((Boolean)"false" == true)?"true":"false";
echo "<br />";
echo ((Boolean)"true" == true)?"true":"false";
echo "<br />";
echo ((Boolean)"true" == false)?"true":"false";
echo "<br />";
echo ((Boolean)"0" == true)?"true":"false";
echo "<br />";
echo ((Boolean)"0" == false)?"true":"false";
?>
于 2013-10-21T19:28:27.433 回答
1

如果没有双亲,您将转换$test为布尔值。

字符串始终评估为布尔值 true,除非它们的值被 PHP 视为“空”(取自 的文档empty):

  1. ""(一个空字符串)
  2. "0"(0 作为字符串)

因此,在您的情况下,PHP 正在解释:

  1. $isInsert3 = (boolean) $test == "update";
  2. $isInsert3 = ((boolean) $test) == "update";
  3. $isInsert3 = true == "update";
  4. $isInsert3 = true == true;
于 2013-10-21T19:27:36.543 回答
1

该问题与运算符优先级有关。实际上 (boolean) 是比比较具有更高优先级的运算符。这意味着第三行相当于

$tmp = (boolean) $test; //true
$isInsert3 = (bool == "update"); // boolean true and non-empty string are treated as equal when you do ==
于 2013-10-21T19:29:10.410 回答
0

这种意外行为的原因是,在您的第三个实例中,您正在转换$test为布尔值,而不是$test == "update".

将“插入”转换为布尔结果为真 - 任何非空字符串都计算为真。然后true == "string"评估为真,因为布尔值与其他类型的比较将两种类型都视为布尔值。同样,像“string”这样的非空字符串等价于真,因此true == true,毫不奇怪,它等于真。

脚注 - 如果您将两个变量与类似的运算符进行比较==,则无需将结果转换为布尔值。结果将始终为布尔值。

于 2013-10-21T19:28:30.170 回答