2

Warning: count(): Parameter must be an array or an object that implements Countable in...

I'm getting the above error on the following line.

if (0 >= count($this->xprop))

Can someone help me understand this? I'm fairly new to PHP. The issue is obviously with $this->xprop not being an array. It wasn't an issue before I upgraded to PHP 7.2. How can I get around this? Are code with warnings still being executed or is this going to cause execution to fail?

I've tried to follow the second answer on here with no luck. The accepted answer is not acceptable to me as it is more of a hack.

DanCue
  • 619
  • 1
  • 8
  • 17
  • 3
    Rather than attempt to ignore the error, you should try to fix the problem given that it will most likely effect the running of your app. – Phil Aug 21 '18 at 23:29
  • Do you have any other errors in the error log? – RiggsFolly Aug 21 '18 at 23:29
  • @Phil That's why I didn't like the acceptable answer on that post. I want to fix this. – DanCue Aug 21 '18 at 23:30
  • 2
    See [the migration guide](http://php.net/manual/en/migration72.incompatible.php) and do a find on `Warn when counting non-countable types` – RiggsFolly Aug 21 '18 at 23:31
  • @RiggsFolly I have other count warnings. All similar to this one with someone's help. I figured if I solve this one I can figure out the rest. – DanCue Aug 21 '18 at 23:31
  • My guess is that WordPress or at least the version of it you are using in not ready for PHP7.2 yet – RiggsFolly Aug 21 '18 at 23:33
  • @RiggsFolly It's a plugin that is throwing the errors. 4 errors total. – DanCue Aug 21 '18 at 23:36
  • Did you upgrade WP before upgrading PHP – RiggsFolly Aug 21 '18 at 23:36
  • So I would look for an update for the plugin – RiggsFolly Aug 21 '18 at 23:36
  • No. PHP was 7.2 before I installed WP on this site. – DanCue Aug 21 '18 at 23:37
  • @RiggsFolly I'm going to try to fix it. At this point I don't see an update. Would be a good learning experience. Thank you for the guide. – DanCue Aug 21 '18 at 23:38
  • 2
    Good luck, that may not be as simple as it sounds :) WP Code is the nastiest code I have seen in my life – RiggsFolly Aug 21 '18 at 23:39
  • You could maybe try to clone $this->xprop and cast it to an array and count it. Kind of hacky but it's WP so you're allowed – billrichards Aug 22 '18 at 00:17
  • 1
    Can you advise what `$this` is? Just because it's in WP doesn't mean it's "WordPress code". Better would be to edit the answer and include the results of `var_dump($this->xprop)`. Not to dispute RiggsFolly - major props to him - but I actually doubt this is WP core code.... – random_user_name Aug 22 '18 at 00:29
  • Or - in lieu of posting the `var_dump`, share the _full error message_. Error messages include lots of valuable information, including which file (and often a backtrace) of where the issue lies. – random_user_name Aug 22 '18 at 00:30
  • 1
    You *should* alter all of these checks to: `is_array($this->xprop) ? count($this->xprop) : 0;` instead of trying to hack / alter the way Countable is interfaced / how and whether or not PHP notifications / warnings. But then, as with much of programming; that is just my opinion. – Grant Aug 22 '18 at 00:33
  • 1
    @Grant - while that would be optimal, inside of WordPress plugins, it's not always possible / practical to refactor the code. As soon as the author rolls out a new update, your changes are vaped.... – random_user_name Aug 22 '18 at 00:35
  • 2
    If it's a plugin, get in touch with the maintainers and / or file a bug report – Phil Aug 22 '18 at 00:39

4 Answers4

4

PHP 7.2 throws an error when trying to count, or get the size of, a variable that isn't set. In previous versions, authors would shortcut checking to see if the variable was set by just counting (or sizeof'ing) it, and getting "0" on an unset variable.

The solution is to check to see if it's set before counting it:

if (isset($this->xprop) && count($this->xprop) == 0)

Your example above is actually negative logic, and is a pretty strange way of stating "if the size of this array is zero, or less than zero" (which is impossible). So, following your example above, the PHP 7.2 compliant version would be to use empty:

if (empty($this->xprop))

... because a variable can be set ($xprop = array()) but still be empty.

Sorry if this is a bit unclear; it's late here!

Foul

FoulFoot
  • 655
  • 5
  • 9
  • 2
    `count($this->xprop) > 0` <- you've reversed the original logic here. Probably not what you want to do – Phil Aug 22 '18 at 02:40
  • Nope -- look at it again. :) The logic in the OP is screwy. "If zero is greater than or equal to the count of the variable"... more clearly stated, "if the count of the variable is equal to or less than zero", or better, "if the count of the variable is zero" (because it can't be less than zero). – FoulFoot Aug 22 '18 at 11:31
  • 1
    `0 >= n` is equivalent to `n <= 0` which is quite different to what's in your answer. `empty()` is a good call though – Phil Aug 22 '18 at 13:01
  • 1
    Oh I see, you're talking about my example with the isset. Yes, that logic is incorrect; I just wanted to demonstrate that you need to check to see if the variable is set before you attempt to count it. The second example, if `(empty($this->xprop))`, is correct and maintains the intended logic. – FoulFoot Aug 22 '18 at 17:04
  • @FoulFoot Thank you for the answer and explanation. That worked beautifully. – DanCue Aug 22 '18 at 22:43
1

the problem is caused because of the PHP version.

In PHP 7.2 , the count() method does not support null as argument .

Example :

in PHP 5.6.x :

echo count(null); // this show 0 

in PHP 7.2.x :

echo count(null); // count(): Parameter must be an array or an object that implements Countable 

So you should verify if the variable is not null

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43
1

if you are using php7.3 or above you can use is_countable before the count

rfc/counting_non_countables

Gehad Mohamed
  • 131
  • 1
  • 6
1

There are some ways, but I like the new ??-operator, because it is short:

$null = null;

echo count($null);                           // Warning: count(): Parameter must be an array or an object that implements Countable
echo is_countable($null) ? count($null) : 0; // => 0
echo count((array)$null);                    // => 0
echo count($null ?? []);                     // => 0

dipser
  • 442
  • 3
  • 5