【问题标题】:Empty string not equal to empty string in PHP ternary operator expressionPHP三元运算符表达式中的空字符串不等于空字符串
【发布时间】:2018-05-23 10:31:46
【问题描述】:

有人可以解释一下吗?

$a="";

$a="" ? "" : "muh";

echo $a; 
// returns muh

【问题讨论】:

  • 空白字符串计算结果为假。
  • 好的,这就是答案。谢谢!

标签: php string ternary-operator is-empty


【解决方案1】:

看起来您正在尝试使用 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; //     

【讨论】:

    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2018-09-05
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多