Author Topic: nws-alerts.php and PHP 7.2 warning  (Read 1724 times)

0 Members and 1 Guest are viewing this topic.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
nws-alerts.php and PHP 7.2 warning
« on: December 28, 2017, 05:48:21 PM »
Can anyone identify what needs to be changed to stop this php warning in Curly's nws-alerts.php script?

"PHP Warning:  count(): Parameter must be an array or an object that implements Countable in C:\\www\\nws-alerts.php on line 581"

There has been a change in PHP starting with version 7.2 that is discussed here: http://php.net/manual/en/migration72.incompatible.php

My concern is that PHP will eventually disallow this use of count() altogether rendering the script useless without a fix.  Thoughts?

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: nws-alerts.php and PHP 7.2 warning
« Reply #1 on: December 28, 2017, 08:34:59 PM »
Could you post the code segment and note which count() call this refers to?

https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: nws-alerts.php and PHP 7.2 warning
« Reply #2 on: December 28, 2017, 09:51:33 PM »
I'll get that and post asap.

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: nws-alerts.php and PHP 7.2 warning
« Reply #3 on: December 29, 2017, 03:26:31 AM »
Can anyone identify what needs to be changed to stop this php warning in Curly's nws-alerts.php script?

"PHP Warning:  count(): Parameter must be an array or an object that implements Countable in C:\\www\\nws-alerts.php on line 581"

There has been a change in PHP starting with version 7.2 that is discussed here: http://php.net/manual/en/migration72.incompatible.php

My concern is that PHP will eventually disallow this use of count() altogether rendering the script useless without a fix.  Thoughts?
it is always EXTREMELY helpful to have a link to the site / script.There are numerous versions and they can be customized also.
But those scripts have a ?sce=view so anyone who wants to help can check and see the offending line.
Probably it is this line:
Code: [Select]
  $icount = count($bigIcos);That variable $bigIcos  is initialised a few lines before as
Code: [Select]
$bigIcos = ''; Up until 7.2, PHP was a loosely typed language and the PHP parser would convert that string to an empty array.
Not anymore, you will find numerous errors like these and others when converting these scripts to 7.2.
Other warnings/notices A non-numeric value encountered in and string to array

For the "it is not an array" errors add a line similar to this one, just before the error
Code: [Select]
if (!is_array($bigIcos) ) {$bigIcos = array();}
For "A non-numeric value" error change the variable-definition, often at lines 117-127 of the script, from
Code: [Select]
$cmyzc   = '';  to
Code: [Select]
$cmyzc   = 0; Wim

P.S. latest version of nws-alerts.php is Version 1.37 - 03-Aug-2016    Add file get contents if cURL is not available
But that one returns the same notices/warnings.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: nws-alerts.php and PHP 7.2 warning
« Reply #4 on: December 29, 2017, 08:29:39 AM »
NWS-Alerts.php Version 1.41 - 07-Mar-2017

https://mymishawakaweather.com/nws-alerts.php?sce=view

Before I saw your post I changed line 573 from this:
Code: [Select]
      $bigIcos = '';

to this:
Code: [Select]
$bigIcos = array();
Since I made that change I haven't seen the error again.  Do you think that this is an acceptable way to fix the script?

« Last Edit: December 29, 2017, 09:12:11 AM by tmabell »

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: nws-alerts.php and PHP 7.2 warning
« Reply #5 on: December 30, 2017, 08:19:47 AM »
@Wim

 I'm interested in your thoughts on this approach.  Thank you.

Tom

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: nws-alerts.php and PHP 7.2 warning
« Reply #6 on: December 30, 2017, 12:19:19 PM »
@Wim

 I'm interested in your thoughts on this approach.  Thank you.

Tom
It is OK, a quick scan of the source revealed that $bigIcos is not used elsewhere.

Succes, Wim

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: nws-alerts.php and PHP 7.2 warning
« Reply #7 on: December 30, 2017, 12:36:19 PM »
Thanks Wim, Happy New Year!