battery.cord battery.cord - 1 month ago 7
PHP Question

Order of evaluation in ternary operator

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.

Answer

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>';