drxm5014
drxm5014
2016-10-17 15:05

三元运算符中的评估顺序

已采纳

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 dongye3917 5年前

    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>';
    
    点赞 评论 复制链接分享
  • dongtu4028 dongtu4028 5年前

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

    This happens becuase for php order here is like this:

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

    So you always add your content to your '' string, so it's never null and returns $content . '</li>'

    lets make code clearer:

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

    As you can see, $a is never null so answer is always $c

    点赞 评论 复制链接分享

为你推荐