-1

I'm updating old software to remove errors and warnings in PHP 7.4 (I have not written a single line of code in over 15 years so consider me completely new).

The offending code is:

if ( count($forum_moderators[$forum_id]) > 0 )
{
    $l_moderators = ( count($forum_moderators[$forum_id]) == 1 ) ? $lang['Moderator'] : $lang['Moderators'];
    $moderator_list = implode(', ', $forum_moderators[$forum_id]);
}
else
{
    $l_moderators = ' ';
    $moderator_list = ' ';
}

If there is a user designated as a moderator, there is no error; but, if the count returns "0", the error is:

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

This seems like it should have a simple answer but everything I have tried just adds more warnings or errors.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
JWI1965
  • 9
  • 1
  • 3
    What is `$forum_moderators[$forum_id]`? It's apparently not something that can be counted – aynber May 19 '23 at 19:06
  • 3
    It should, but apparently isn't, or maybe not all the time. You can double-check with `var_dump($forum_moderators[$forum_id]);` or if you have logging, use that to get the details of that variable. – aynber May 19 '23 at 19:20
  • thank you to aynber for attempting to help me but unfortunately stackoverflow has deteriorated over the years to become completely useless to anyone who is not an expert programmer with a very unique question. – JWI1965 May 19 '23 at 21:32
  • I have considerable doubts regarding the uniqueness of this question. – Your Common Sense May 20 '23 at 09:19
  • 1
    @YourCommonSense Then find a **real** duplicate. Not that "catch all" target which doesn't even mention the warning cited here. – Adrian Mole May 21 '23 at 14:56
  • While I have some sympathy for your frustration, you have to also consider the task of those trying to answer: there are well over a million PHP questions already on this site, and dozens of new ones raised every day. You mention "everything you've tried", but not what any of that is; you don't mention any searches you've done for previous questions; and the code you've shown is not a [mre]. That passes a lot of effort to those trying to help you. – IMSoP May 21 '23 at 15:02
  • @AdrianMole check your facts please. 1. That "catch all" [does](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/56011600#56011600) mention the warning cited here. 2. I didn't close this question with this target. 3. You are as much a participant here as I. So you can find that duplicate as well. A duplicate which features exactly the same patch to circumvent the error instead of fixing it, as provided for this newly opened question. – Your Common Sense May 21 '23 at 15:20
  • @YourCommonSense Oops! But the fact that I missed it among the huge list of errors in that mega-canonical highlights the issue with such dupe targets. – Adrian Mole May 21 '23 at 15:28
  • @YourCommonSense FYI, that mega-dupe is being discussed in a [chat room](https://chat.meta.stackexchange.com/rooms/1701/meta-stack-exchange). – Adrian Mole May 21 '23 at 15:32
  • @AdrianMole thank you but I am not related to that dupe in any way – Your Common Sense May 21 '23 at 15:46

1 Answers1

-1

So, it looks like the problem occurs because you have not an array passed to this code sometimes.

You could try to add a statement in the condition :

if (is_array($forum_moderators[$forum_id]) && count($forum_moderators[$forum_id]) > 0 )

Tell us if this working or what is the new error.

unshiny99
  • 141
  • 6