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 of false
.
- 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 that 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.)
var_dump($this->item->extra_fields)
to show what is not countable.!empty()
will suffice, but if it has some random string in it, then that is another problem. We need more diagnostic details or we are just assuming/guessing at resolutions. Callvar_dump()
before the error and thenexit()
. I am likely to recommendif($this->item->params->get('itemExtraFields') && !empty($this->item->extra_fields)):
but I need more clarity before I do.count()
method of PHP in the code in line 398. Ifcount()
method is used it can only be used with an array or a countable object, otherwise an E_WARNING will be emitted since PHP7.2: php.net/manual/en/migration72.incompatible.php