PHP 布尔转换很尴尬


PHP boolean casting awkward

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

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

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

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

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

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")?那么它的FALSE!

(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";
?>

如果没有括号,您将$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;

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

$tmp = (boolean) $test; //true
$isInsert3 = (bool == "update"); // boolean true and non-empty string are treated as equal when you do ==

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

将"insert"转换为布尔值会导致 true - 任何非空字符串的计算结果为 true。 然后true == "string"计算结果为 true,因为将布尔值与其他类型进行比较会将这两种类型都视为布尔值。 同样,像"string"这样的非空字符串等价于true,所以true == true,毫不奇怪地等于true。

脚注 - 如果要使用像 == 这样的运算符比较两个变量,则不需要将结果转换为布尔值。 结果将始终是布尔值。