Author Topic: PHP 7.1 gets testy about string arithmetic operators  (Read 1614 times)

0 Members and 1 Guest are viewing this topic.

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
PHP 7.1 gets testy about string arithmetic operators
« on: September 18, 2017, 10:41:24 AM »
In my web site "sandbox" (where I test my mods before going live) I have PHP error reporting turned on in the most verbose mode possible. Last night, I noticed that I was getting a "Notice" message in the METAR code that I hadn't seen before. Ends up it was a combination of PHP 7.1 (which I'm using) and fog...

  Notice: A non well formed numeric value encountered in (file) (line number)

After digging a bit, I found that the line generating the notice is in get-metar-conditions-inc.php within the function mtr_get_visibility():

Code: [Select]
$kmVis = round( $part * 1.6 );
and it only occurs when $part is a fraction (e.g, "1/2", "1/4").

After even more investigation, I see that it consistently gives me the Notice only when pulling the data from the cache file. When pulling fresh data from the web, the Notice doesn't happen - even though the string is the same.  I stuck some debug code in to put the string, its length, and its type into the comments. There is no difference in what is displayed in the comment between the cached and fresh data (e.g., "1/4", length 4, type "string"), and yet I only see the error for the cached data.

As to why I'm only seeing this now, it's a change that went into PHP 7.1.

  https://wiki.php.net/rfc/invalid_strings_in_arithmetic

Anyway, I'm still trying to figure this out.  It's not a huge problem as the math still works properly whether the Notice appears or not, but it's bugging me.... If anyone has an idea let me know.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9257
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #1 on: September 18, 2017, 11:41:48 AM »
Thanks for the heads-up.. I'll look at a fix for get-metar-conditions-inc.php
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #2 on: September 18, 2017, 11:43:56 AM »
One mystery solved: I don't see the notice when the data is fetched from the web because the fetch routine overrides my error reporting setting.

Code: [Select]
   // Suppress error reporting so Web site visitors are unaware if the feed fails
   error_reporting(0);

So... the message is triggered any time a fraction string of the form "x/y" is used in an arithmetic operation. But the arithmetic still works.

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

Offline R_o_B

  • WxElement panel
  • Senior Member
  • *****
  • Posts: 85
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #3 on: September 20, 2017, 07:53:35 AM »
Just curious...

You wrote that the $part variable give you the following information (from your example): "1/4", length 4, type "string"

The $part variable is part of the following code snippet: $kmVis = round( $part * 1.6 );

In my view, for the 'round' function to produce the right results, the $part variable would need to be a numeric value and not a string. So, when the $part variable as a numeric value is multiplied by the numeric value 1.6, then the rounding of the multiplication should proceed as expected.

I just tried it and I got rid of the error. Here is how:
Code: [Select]
if ( is_numeric( $part ) == false ) {
  list ( $numerator, $denominator ) = explode( '/', $part );
  $part = ( $numerator / $denominator );
}
The '1/4' string value is transformed into the '0.25' numeric value.

Do I make sense...  :-|
R_o_B
---
eMail: weather@herray.net

Offline weather34

  • Forecaster
  • *****
  • Posts: 1068
    • https://weather34.com/homeweatherstation
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #4 on: September 20, 2017, 08:22:58 AM »
Makes total sense , with an upgrade to 7.1.8 from 5.6 I had to recently on another non weather project had to convert values like 1/4 , 1/2 into numerical I simply added or converted them by using for example
if ($x == 1/4){$x = 0.25};
if ($x == 1/2){$x = 0.50};
And so on.....
else $x ;

$x is just a reference...

7.2 also a few little quirks but I don’t think it will cause too much headache..brian


« Last Edit: September 20, 2017, 08:26:00 AM by weatherist34 »

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #5 on: September 20, 2017, 08:40:55 AM »
You wrote that the $part variable give you the following information (from your example): "1/4", length 4, type "string"


Oops - that should be length 3, I fat-fingered that, but yeah...

Quote
Code: [Select]
if ( is_numeric( $part ) == false ) {
  list ( $numerator, $denominator ) = explode( '/', $part );
  $part = ( $numerator / $denominator );
}
The '1/4' string value is transformed into the '0.25' numeric value.

Do I make sense...  :-|

Yup.  I did an explicit search for the '/' but that should work, at least if PHP is acting consistent...  Paranoid me added "floatval()" calls around the numerator and denominator.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: PHP 7.1 gets testy about string arithmetic operators
« Reply #6 on: September 20, 2017, 08:47:43 AM »
One other thing... $part gets used a few lines down and is expected to still be in string format to set $mtr_info['VISIBILITY'] (if units are miles), so I use a different variable ($mult) for the numeric flavor of $part.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

 

anything