I'm working on making a walker in Wordpress. I saw some example code and it had the following syntax on one of it's lines:
!empty($item->attr_title)
and $attributes .= ' title="'.esc_attr($item->attr_title).'"';
What does this do? I would assume that if the object attributes are not empty it appends the title string, but I'm not sure the role of the "and" word here.
PHP does short circuit evaluation of boolean expressions, that is, only as many terms are evaluated until the result is definite.
true and something()
will evaluate something() (the complete expression could still eval to false), whereas
false and something()
will stop evaluating after the false term.
That's a shorthand for:
if (!empty($item->attr_title)) {
$attributes .= ' title="'.esc_attr($item->attr_title).'"';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With