gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php中三元运算符的嵌套实例

来源:http://erdangjiade.com/topic/1209.html 你好,世界。 2017-09-25 22:42浏览(274)

// 乍看起来下面的输出是 'true'
echo (true?'true':false?'t':'f');

// 然而,上面语句的实际输出是't',因为三元运算符是从左往右计算的

// 下面是与上面等价的语句,但更清晰
echo ((true ? 'true' : 'false') ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
/**
 *先判断$_GET['a']若成立则判断(isset($_GET['b']) ? $_GET['b'] : 'other'),因为有括号,所以
 *会当成一个整体运算
 */
$rs = isset($_GET['a']) ? $_GET['a'] : (isset($_GET['b']) ? $_GET['b'] : 'other');
var_dump($rs);


/**
 * 注意:自左向右结合运算
 *先判断$_GET['a']若成立则 则变成 $_GET['a'] ? $_GET['b'] : 'other';
 *不可忽视括号的作用
 */
$rs = isset($_GET['a']) ? $_GET['a'] : isset($_GET['b']) ? $_GET['b'] : 'other';
var_dump($rs);

以上就是php中三元运算符的嵌套实例的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2