SimplePortal

Development => Bugs => Topic started by: KathyL on July 08, 2017, 06:27:55 AM

Title: PHP 7.1 : A non well formed numeric value encountered
Post by: KathyL on July 08, 2017, 06:27:55 AM
Since the implementation of PHP 7.1 warnings are issued about invalid strings in arithmetic (https://wiki.php.net/rfc/invalid_strings_in_arithmetic).

For SMF 2.0.14/Simple Portal 2.3.6/PHP 7.1 this has resulted in 'A non well formed numeric value encountered' error messages on my forums whenever changes to blocks are saved because of the presence of non-numeric characters (eg, '%', 'px').

The workaround I implemented in ./Sources/PortalAdminBlocks.php is as follows (ie, insert '(float)' before each width value used in the calculations to return only the numeric value):
Find
Code: [Select]
if (strpos($context['widths'][1], '%') !== false)
$context['widths'][2] = $context['widths'][3] = 100 - ($context['widths'][1] + $context['widths'][4]) . '%';
elseif (strpos($context['widths'][1], 'px') !== false)
$context['widths'][2] = $context['widths'][3] = 960 - ($context['widths'][1] + $context['widths'][4]) . 'px';

if (strpos($context['widths'][1], '%') !== false)
{
$context['widths'][2] = $context['widths'][3] = 100 - ($context['widths'][1] + $context['widths'][4]) . '%';
$context['widths'][5] = $context['widths'][6] = '100%';
}
elseif (strpos($context['widths'][1], 'px') !== false)
{
$context['widths'][2] = $context['widths'][3] = 960 - ($context['widths'][1] + $context['widths'][4]) . 'px';
$context['widths'][5] = $context['widths'][6] = '960px';
}
Replace with:
Code: [Select]
if (strpos($context['widths'][1], '%') !== false)
$context['widths'][2] = $context['widths'][3] = 100 - ((float)$context['widths'][1] + (float)$context['widths'][4]) . '%';
elseif (strpos($context['widths'][1], 'px') !== false)
$context['widths'][2] = $context['widths'][3] = 960 - ((float)$context['widths'][1] + (float)$context['widths'][4]) . 'px';

if (strpos($context['widths'][1], '%') !== false)
{
$context['widths'][2] = $context['widths'][3] = 100 - ((float)$context['widths'][1] + (float)$context['widths'][4]) . '%';
$context['widths'][5] = $context['widths'][6] = '100%';
}
elseif (strpos($context['widths'][1], 'px') !== false)
{
$context['widths'][2] = $context['widths'][3] = 960 - ((float)$context['widths'][1] + (float)$context['widths'][4]) . 'px';
$context['widths'][5] = $context['widths'][6] = '960px';
}
Title: Re: PHP 7.1 : A non well formed numeric value encountered
Post by: Chen Zhen on July 09, 2017, 08:14:22 AM

Thanks for the report although intval may be for suitable than float.
Title: Re: PHP 7.1 : A non well formed numeric value encountered
Post by: KathyL on July 11, 2017, 09:57:32 PM

Thanks for the report although intval may be for suitable than float.
Thanks for the suggestion - I'll update my code.

Update:  I tried 'intval' but it generated "syntax error, unexpected '$context' (T_VARIABLE)" errors - 'int' seems to be working though.
Title: Re: PHP 7.1 : A non well formed numeric value encountered
Post by: emanuele on July 12, 2017, 07:20:43 AM
intval is a function
int is a type
Both are equivalent in the outcome, but you use them differently:
Code: [Select]
$something = intval($something);
$something = (int) $something;
Title: Re: PHP 7.1 : A non well formed numeric value encountered
Post by: Chen Zhen on July 12, 2017, 08:41:31 PM

For a better understanding, you can use php.net for a reference:
http://php.net/manual/en/function.intval.php

SimplePortal 2.3.7 © 2008-2021, SimplePortal