PHP 三元运算符表达式中的空字符串不等于空字符串
Empty string not equal to empty string in PHP ternary operator expression
有人能给我解释一下吗?
$a="";
$a="" ? "" : "muh";
echo $a;
// returns muh
看起来您正在尝试使用 Comparison operator ==
, but instead you are using an Assignment operator =
您的代码试图将 $a
表达式 "" ? "" : "muh"
的结果赋值。空字符串被评估为 false
并且 $a
被赋予 muh
.
的值
让我们加一些括号使其更明显:
//$a equals (if empty string then "" else "muh")
$a = ("" ? "" : "muh");
echo $a; // muh
//$a equals (if $a is equal to empty string then "" else muh)
$a = ($a == "" ? "" : "muh");
echo $a; //
有人能给我解释一下吗?
$a="";
$a="" ? "" : "muh";
echo $a;
// returns muh
看起来您正在尝试使用 Comparison operator ==
, but instead you are using an Assignment operator =
您的代码试图将 $a
表达式 "" ? "" : "muh"
的结果赋值。空字符串被评估为 false
并且 $a
被赋予 muh
.
让我们加一些括号使其更明显:
//$a equals (if empty string then "" else "muh")
$a = ("" ? "" : "muh");
echo $a; // muh
//$a equals (if $a is equal to empty string then "" else muh)
$a = ($a == "" ? "" : "muh");
echo $a; //