除非有括号,否则php中的三元运算符会设置变量错误

时间:2018-05-18 04:44:29

标签: php ternary-operator

三元运算符如何在php中工作似乎有点奇怪,有人可以解决这个问题吗?

$z = 'testval';
$x = 'hello ' . isset($z) ? $z : ' notfound';

var_dump($x);  //testval

这导致了预期的结果:

$z = 'testval';
$x = 'hello ' . (isset($z) ? $z : ' notfound');

var_dump($x); //returns hello testval

0 个答案:

没有答案