2

After upgrading to PHP 7.2 I get a lot of Warnings

Parameter must be an array or an object that implements Countable

I've tried many ways to fix it but none have worked so far. The commented lines below are my attempts. How can I fix this?

$bannerGroupCode = $this->getBannerGroupCode();
$data = $this->getDataByGroupCode($bannerGroupCode);
$bannerGroupData = $data['group_data'];
$template = '';
  if (count($bannerGroupData) > 0):
// if (empty($bannerGroupData)):
// if ($bannerGroupData && count($bannerGroupData) >= 0):
// if (is_array($bannerGroupData) && count($bannerGroupData) > 0):
// if (strlen($bannerGroupData) > 0):
    $prebaneff = $bannerGroupData->getPreBannerEffects();
    $bannerType = $bannerGroupData->getAnimationType();
2
  • Have you tried ((is_object($bannerGroupData) || is_array($bannerGroupData)) && count($bannerGroupData) > 0) ? May 8, 2019 at 13:37
  • Yes, I just tried it and it gives the same error.
    – Freejoy
    May 8, 2019 at 13:44

1 Answer 1

2

There is a function available in PHP (>=7.3) which checks if a variable is countable is_countable

Try it like

if (is_countable($bannerGroupData) && count($bannerGroupData) > 0):
    // Your code
endif;

If the function is not available, you can use below

if (($bannerGroupData instanceof Countable) || is_array($bannerGroupData)) && count($bannerGroupData) > 0):
    // Your code
endif;
4
  • The first suggestion gives the following error: PHP Fatal error: Uncaught Error: Call to undefined function is_countable(). The second suggestion makes the banner not show.
    – Freejoy
    May 8, 2019 at 14:00
  • @Freejoy Please find updated code, the first one may not work because the function is added in PHP 7.3. Please try with second code now and let me know if you still face the issue. May 9, 2019 at 4:38
  • Darn, it still doesn't work. I get a blank screen. I don't see any errors in the log files. Thanks!
    – Freejoy
    May 9, 2019 at 14:28
  • I found the problem with the last code. There was a missing "( " So I added the code: if ((($bannerGroupData instanceof Countable) || is_array($bannerGroupData)) && count($bannerGroupData) > 0): and the blank screen was corrected but the banner stopped working.
    – Freejoy
    May 10, 2019 at 14:22

Your Answer

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

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