Since the value of $this->item->extra_fields
is either an array or null
, you don't need to check if it "is set" with !empty()
or isset()
; a truthy value check is all that is required.
if ($this->item->params->get('itemExtraFields') && $this->item->extra_fields) {
By writing $this->item->extra_fields
(without any function calls or operators) into the conditional expression, it is checked for "truthiness" or in other words checking its boolean value.
null
has a boolean value offalse
.- an empty array has a boolean value of
false
. - a non-empty array has a boolean value of
true
.
Here is a quick demonstration.
So, if the extra_fields
property is not null and not an empty array, then it will pass the conditional criteria and enter thanthat branch of the condition block.
If there is any possibility that $this->item->extra_fields
might not be set, then the safer technique is to call !empty()
on it. Using !empty()
for ALL falsey checks in a project will lead to loads of unnecessary code bloat. If you are assured that a variable will be declared/set, then don't make the function call. (Objects are a different matter and cannot be treated the same way, but I will not digress.)