0

Warning: count(): Parameter must be an array or an object that implements Countable in /home/......./archi/html/com_k2/templates/default/item.php on line 398

 396 <!-- K2 Plugins: K2BeforeDisplayContent -->
 397 <?php echo $this->item->event->K2BeforeDisplayContent; ?>
 398 <?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>

Тhis error appears in the blog part and in particular by entering the separate stratum. Not in the general review, but in the specific article.

5
  • Welcome to JSE. Please take our tour. Please var_dump($this->item->extra_fields) to show what is not countable. Commented Nov 23, 2020 at 8:30
  • This is the file code. github.com/Dmtrv91/siteProblem/blob/main/…
    – dmtrv91
    Commented Nov 23, 2020 at 12:13
  • We need to know what value is held in the variable causing trouble. It should be an array, but it is not. If it is falsey, then !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. Call var_dump() before the error and then exit(). I am likely to recommend if($this->item->params->get('itemExtraFields') && !empty($this->item->extra_fields)): but I need more clarity before I do. Commented Nov 23, 2020 at 20:17
  • The warning is coming from the count() method of PHP in the code in line 398. If count() 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
    – Zollie
    Commented Nov 23, 2020 at 21:39
  • I hope @Lodder will extend his answer on this too...
    – Zollie
    Commented Nov 24, 2020 at 5:25

3 Answers 3

1

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.)

0
0

$this->item->extra_fields is most likely empty/null and therefore not an array or object, so PHP cannot count it.

I believe you can add an additional check to see if it's empty and set, like so:

<?php if (!empty($this->item->extra_fields) && isset($this->item->extra_fields)) : ?>
    <?php if ($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)) : ?>
        // Do something
    <?php endif; ?>
<?php endif; ?>
10
  • Probably in this case it is better to pre-check whether the $var is countable too with using is_countable($var) method.
    – Zollie
    Commented Nov 23, 2020 at 18:29
  • It is never necessary to write both checks (isset() and !empty()) in a condition: !empty($this->item->extra_fields) && isset($this->item->extra_fields). stackoverflow.com/a/4559976/2943403 If !empty($this->item->extra_fields) is satisfied, then isset($this->item->extra_fields) will always be satisfied. If a variable is declared/set (and there is evidence that it is because there is no warning of accessing an undeclared property), then checking an array for non-emptiness is as simple as if ($this->item->extra_fields) { Commented Nov 23, 2020 at 20:11
  • It is never necessary to write both checks. That's completely incorrect. They are two different methods, performing two different tasks. So yes, you can use them in the same condition if you need to. !empty() will reject 0, which is a completely valid value.
    – Lodder
    Commented Nov 24, 2020 at 6:08
  • You're both welcome to write additional answers
    – Lodder
    Commented Nov 24, 2020 at 6:11
  • @mickmackusa - I thought that you will notice that (you did it few times earlier at other posts and I agree with you mostly on that) however as @Lodder mentioned there are few cases where we should use both checks together, like when you have an empty string for example: ”” the variable is set. So the isset() is satisfied (because the value is not null) but the !empty() condition is not satisfied. So there can be few exceptions from the “never”...
    – Zollie
    Commented Nov 24, 2020 at 6:54
0

Now I had time to look at the feedback/answer.

I checked the value with var_dump() and it showed NULL.

The problem was solved with a correction of the check which @mickmackusa mentioned:

<?php if ($this->item->params->get('itemExtraFields') && !empty($this->item->extra_fields)): ?>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.