drxm5014 2016-10-17 07:05
浏览 22
已采纳

三元运算符中的评估顺序

I have two snippets of php:

function prefix_shortcode_statement_conditional( $atts, $content = null ) {
    return '<li class="some-class">' . $content == null ? '' : $content . '</li>';
}

function prefix_shortcode_statement( $atts, $content = null ) {
    return '<li class="some-class">' . $content . '</li>';
}

The first snippet has a conditional statement to return an empty string as the <li> content, while the second one merely returns the content wrapped in <li>.

prefix_shortcode_statement_conditional output

Lorem Ipsum ...

prefix_shortcode_statement output

<li class="some-class">
    Lorem Ipsum ... 
</li>

As you can see, only the function without the conditional statement properly generates the <li> element. Why does one snippet generate the code, but not the other?


Wrapping the conditional in parentheses resolves the issue, but fails to help me understand why the issue arose in the first place.

return '<li class="some-class">' . ($content == null ? '' : $content) . '</li>';

As far as I can tell, php should interpret ending </li> as part of the conditional statement, but fails to. Instead of adding $content . '</li>' to the output, it instead just returns $content and at some level (browser, wordpress, etc), the first <li> is removed from the outputted html.

Interestingly, wrapping the $content and the </li> also fails to display the <li>...</li>.

return '<li class="mep-testimonial-li">' . $content == null ? '' : ($content . '</li>');

I would expect to only see this issue when $content is null, so the referenced </li> would be omitted and the formatting would shift.

展开全部

  • 写回答

2条回答 默认 最新

  • dongye3917 2016-10-17 07:15
    关注

    In PHP, as in other languages, concatenation have a higher precedence than comparison, so:

    return '<li class="some-class">' . $content == null ? '' : $content . '</li>';
    

    Will always print $content . '</li>', since '<li class="some-class">' . $content will never be falsy ou null.
    This may lead to invalid markup and not be rendered correctly in the browser.

    Always wrap your ternaries in parenthesis.

    return '<li class="some-class">' . ($content == null ? '' : $content) . '</li>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部