Development > Bugs

PHP 7.1 : A non well formed numeric value encountered

(1/1)

KathyL:
Since the implementation of PHP 7.1 warnings are issued about 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: --- 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';
}

--- End code ---
Replace with:

--- Code: --- 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';
}

--- End code ---

Chen Zhen:

Thanks for the report although intval may be for suitable than float.

KathyL:

--- Quote from: Chen Zhen on July 09, 2017, 08:14:22 AM ---
Thanks for the report although intval may be for suitable than float.

--- End quote ---
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.

emanuele:
intval is a function
int is a type
Both are equivalent in the outcome, but you use them differently:

--- Code: ---$something = intval($something);
$something = (int) $something;
--- End code ---

Chen Zhen:

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

Navigation

[0] Message Index

Go to full version