【发布时间】:2018-05-23 10:31:46
【问题描述】:
有人可以解释一下吗?
$a="";
$a="" ? "" : "muh";
echo $a;
// returns muh
【问题讨论】:
-
空白字符串计算结果为假。
-
好的,这就是答案。谢谢!
标签: php string ternary-operator is-empty
有人可以解释一下吗?
$a="";
$a="" ? "" : "muh";
echo $a;
// returns muh
【问题讨论】:
标签: php string ternary-operator is-empty
看起来您正在尝试使用 Comparison operator ==,但您使用的是 Assignment operator =
您的代码正在尝试将表达式"" ? "" : "muh" 的结果分配给$a。一个空字符串被评估为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; //
【讨论】: