Fixed ErrorException: A non-numeric value encountered (php7.1)

Xon

Well-known member
ErrorException: A non-numeric value encountered - library/XenForo/Input.php:244
Code:
#0 /var/www/html/library/XenForo/Input.php(244): XenForo_Application::handlePhpError(2, 'A non-numeric v...', '/var/www/html/...', 244, Array)
#1 /var/www/html/library/XenForo/Input.php(212): XenForo_Input::_doClean('unum', Array, '', 0)
#2 /var/www/html/library/XenForo/Input.php(411): XenForo_Input->filterSingle('renew_price', 'unum')
#3 /var/www/html/library/XenProduct/ControllerPublic/Product.php(669): XenForo_Input->filter(Array)
...

While this was triggered by an add-on; it is a change in behaviour from php 7.1 which affects XenForo_Input;

This is the cause;
Code:
["renew_price"] => string(0) ""

The relevant line is;
Code:
$data = strval($data) + 0;

Wrapping strval in intval, or replacing strval for intval fixes this.
 
I received a similar error in library/XenForo/Application.php's getMemoryLimit. Had to intval $curLimit after "switch (substr($curLimit, -1))" because it didn't like *= on a string.
 
That's a different issue (already reported and fixed).

This one can't be fixed with intval() as it's designed to allow non-integer numbers. Floatval() might work though. I'm not totally sure why I didn't do that initially; I think it was because it would cast an integer to a float whereas the + 0 would give whatever output was reasonable. It's possible we can use both floatval() and + 0. I'll need to test.
 
Oops, missed it was for non-integer numbers. It looks like the original intent was to try parse it as integer-like number and then to a float-like number.

Perhaps doing as is_numeric() check around the strval() is the best solution?
 
This is fixed now. I've maintained the existing behavior of returning an int or a float as appropriate, though I doubt that's a behavior being depended on. The fix is to use floatval first:
Code:
strval(floatval($data)) + 0
Technically, I can just use floatval, but then it always returns a float, even for an integer value.
 
Back
Top Bottom