Upon viewing any webform node (also as anonymous user), the following warning is shown after upgrading to PHP 7.1.

Warning: A non-numeric value encountered in _webform_render_number() (line 318 of /somepath/sites/all/modules/webform/components/number.inc).

The corresponding code is

  if (!is_int($element['#step'] * 1)) { //<-- this line
    $decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
    $element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
  }

The warning is triggered when $element['#step'] is the empty string (or any non-integer). Explicitly casting to int solves it, but I'm not sure if that's the nicest way to fix this bug.

numeric.inc contains many more examples of !is_int($maybeint * 1), so it is likely that the warning can also be triggered in other locations.

Comments

Ko. created an issue. See original summary.

liam morland’s picture

I expect that the error is because of the * 1; it doesn't like multiplying something that isn't a number. Perhaps that variable should always be initialized to an integer. I don't mind casting, but that whole block of code looks suspicious, like it could be rewritten some other way. In particular, I think there ought to be a better way than using string functions to manipulate the number.

pifagor’s picture

StatusFileSize
new3.03 KB

patch

pifagor’s picture

StatusFileSize
new3.13 KB

patch

pifagor’s picture

Status: Active » Needs review

patch

The last submitted patch, 3: webform-2857854-3.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: webform-2857854-4.patch, failed testing.

pifagor’s picture

Status: Needs work » Needs review
StatusFileSize
new1.95 KB

patch

pifagor’s picture

StatusFileSize
new557 bytes

patch

liam morland’s picture

It doesn't make sense to call is_int() on a value that has just been cast to int; it will always be true.

I expect that the * 1 is there to convert to numeric. Casting to float might do what is needed.

pifagor’s picture

StatusFileSize
new565 bytes

patch

pifagor’s picture

StatusFileSize
new2.68 KB

patch

bocaj’s picture

Status: Needs review » Reviewed & tested by the community

#12 worked well for me!

liam morland’s picture

Status: Reviewed & tested by the community » Needs work

While I am sure the new code will get rid of the error, I'm not certain that it has the same functionality. The PHP documentation on floats states that (float) 8 might be internally represented as 7.9999999999999991118. If that is cast to integer, it will be 7. So this code would erroneously state that "8" is not an integer. Perhaps by using round() we can avoid this problem (round() returns a float, so strict comparison is possible):

round($element['#step']) !== (float) $element['#step']

number.inc contains another example of the problematic code which should be fixed by this patch:

if (is_int(($value . '') * 1))

liam morland’s picture

Maybe this would be better:
filter_var((float) $element['#step'], FILTER_VALIDATE_INT) !== FALSE

pifagor’s picture

Status: Needs work » Needs review
StatusFileSize
new3.22 KB

@Liam Morland
This is the best solution I've seen :)

liam morland’s picture

I think we should use strict equality (===) and do that comparison with the last one as well. The eliminates the possibility that zero gets treated differently. We want to know only whether or not the filter failed.

pifagor’s picture

StatusFileSize
new3.23 KB

patch

  • Liam Morland committed be55cd4 on 7.x-4.x authored by pifagor
    Issue #2857854 by pifagor, Liam Morland: Fix non-numeric value warning...
liam morland’s picture

Title: PHP warning non-numeric value encountered with PHP 7.1 » Fix non-numeric value warning in PHP 7.1
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

webservant316’s picture

just ran into this.

jjmackow’s picture

Thanks for the patch. I applied the patch in #28 against version 7.x-4.15 and the issue went away. I'm using PHP 7.1 on a CentOS 7 server

pifagor’s picture

Thank

Mustrum’s picture

#18 worked for me. I'm using PHP 7.1 on Ubuntu server 18.04.
Drupal 7.61