Chazy Chaz
Chazy Chaz

Reputation: 1851

php 7.1 warning "A non-numeric value" only for numeric values "0-4"

I have the following working code:

$nif = 'A12345678';

$even = $nif[2] + $nif[4] + $nif[6];

for ($i = 1; $i < 8; $i += 2)
{
    echo $nif[$i];
    $even += substr((2 * $nif[$i]), 0, 1) + substr((2 * $nif[$i]), 1, 1);
}

Output for PHP 7.0.14

1357

Output for PHP 7.1

1<br />
<b>Warning</b>:  A non-numeric value encountered in <b>[...][...]</b> on line <b>22</b><br />
3<br />
<b>Warning</b>:  A non-numeric value encountered in <b>[...][...]</b> on line <b>22</b><br />
57

Now if I change the value of $nif to:

$nif = 'A52545678';

The output for 7.1 would be:

5557

Already tried passing a normal variable to substr() but that's not the problem:

$a = $nif[$i];
$even += substr((2 * $a), 0, 1) + substr((2 * $a), 1, 1);

Why is PHP 7.1 or substr() from PHP 7.1 treating numeric values in the range '0-4' as non-numeric values?

Upvotes: 1

Views: 3234

Answers (1)

Shira
Shira

Reputation: 6570

The problem lies in the following part of the code:

substr((2 * $nif[$i]), 1, 1)
  1. for $i = 1;, 2 * $nif[$i] will yield 2.
  2. 2 gets casted to a string by substr() as it tries to extract the second character, which doesn't exist, since the string is '2'
  3. substr() yields an empty string
  4. the empty string '' is passed to the + operator, which produces the A non-numeric value encountered warning, since an empty string is not a valid numeric string.

The reason it works with 5 and 7 is that in step 2 substr() will operate on a 2-character string instead of 1-character string and will thus yield a valid numeric string.

This warning is new in PHP 7.1, according to Migrating from PHP 7.0.x to PHP 7.1.x:

Notices and warnings on arithmetic with invalid strings

New E_WARNING and E_NOTICE errors have been introduced when invalid strings are coerced using operators expecting numbers (+ - * / ** % << >> | & ^) or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an E_WARNING is emitted when the string does not contain a numeric value.

You can circumvent this with explicit (int) casts before the substr() calls:

(int) substr((2 * $nif[$i]), 1, 1)

Upvotes: 4

Related Questions