Warning: a Non-Numeric Value Encountered

Recently updated to PHP 7.1 and start getting following error

Warning: A non-numeric value encountered in on line 29

Here is what line 29 looks like

$sub_total += ($item['quantity'] * $product['price']);

On localhost all works fine..

Any ideas how to tackle this or what it is ?

6

24 Answers

Not exactly the issue you had but the same error for people searching.

This happened to me when I spent too much time on JavaScript.

Coming back to PHP I concatenated two strings with + instead of . and got that error.

20

It seems that in PHP 7.1, a Warning will be emitted if a non-numeric value is encountered. See this link.

Here is the relevant portion that pertains to the Warning notice you are getting:

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.

I'm guessing either $item['quantity'] or $product['price'] does not contain a numeric value, so make sure that they do before trying to multiply them. Maybe use some sort of conditional before calculating the $sub_total, like so:

<?php

if (is_numeric($item['quantity']) && is_numeric($product['price'])) {
  $sub_total += ($item['quantity'] * $product['price']);
} else {
  // do some error handling...
}
7

You can solve the problem without any new logic by just casting the thing into the number, which prevents the warning and is equivalent to the behavior in PHP 7.0 and below:

$sub_total += ((int)$item['quantity'] * (int)$product['price']);

(The answer from Daniel Schroeder is not equivalent because $sub_total would remain unset if non-numeric values are encountered. For example, if you print out $sub_total, you would get an empty string, which is probably wrong in an invoice. - by casting you make sure that $sub_total is an integer.)

10

In my case it was because of me used + as in other language but in PHP strings concatenation operator is ..

0

Hello, In my case using (WordPress) and PHP7.4 I get a warning about numeric value issue. So I changed the old code as follow:

From:

$val = $oldval + $val;

To:

$val = ((int)$oldval + (int)$val);

Now the warning disappeared :)

I had this issue with my pagination forward and backward link .... simply set (int ) in front of the variable $Page+1 and it worked...

<?php 
$Page = (isset($_GET['Page']) ? $_GET['Page'] : '');
if ((int)$Page+1<=$PostPagination) {
?>
<li> <a href="Index.php?Page=<?php echo $Page+1; ?>"> &raquo;</a></li>
<?php }
?>
1

This was happening to me specifically on PHPMyAdmin. So to more specifically answer this, I did the following:

In File:

C:\ampps\phpMyAdmin\libraries\DisplayResults.class.php

I changed this:

// Move to the next page or to the last one
$endpos = $_SESSION['tmpval']['pos']
    + $_SESSION['tmpval']['max_rows'];

To this:

$endpos = 0;
if (!empty($_SESSION['tmpval']['pos']) && is_numeric($_SESSION['tmpval']['pos'])) {
    $endpos += $_SESSION['tmpval']['pos'];
}
if (!empty($_SESSION['tmpval']['max_rows']) && is_numeric($_SESSION['tmpval']['max_rows'])) {
    $endpos += $_SESSION['tmpval']['max_rows'];
}

Hope that save's someone some trouble...

I encountered the issue in phpmyadmin with PHP 7.3. Thanks @coderama, I changed libraries/DisplayResults.class.php line 855 from

// Move to the next page or to the last one
$endpos = $_SESSION['tmpval']['pos']
    + $_SESSION['tmpval']['max_rows'];

into

// Move to the next page or to the last one
$endpos = (int)$_SESSION['tmpval']['pos']
    + (int)$_SESSION['tmpval']['max_rows'];

Fixed.

Try this.

$sub_total = 0;

and within your loop now you can use this

$sub_total += ($item['quantity'] * $product['price']);

It should solve your problem.

Check if you're not incrementing with some variable that its value is an empty string like ''.

Example:

$total = '';
$integers = range(1, 5);

foreach($integers as $integer) {
    $total += $integer;
}
1

PHP 7.1-7.4

This warning happens when you have a non-numeric string in an expression (probably +, -, *, or /) where PHP is expecting to see another scalar (int, float, or bool). There are two likely situations where this happens:

  • You did not mean to use an operation that expects a scalar. For example, + (addition) when you meant . (concatenation).
  • You were expecting a number, but the value you used was not even close to a number. Figure out what your non-number is, and handle accordingly.
    • For example, if you have echo 3 + $variable and your $variable is the string "n/a", then you might decide to instead echo "not applicable". Or maybe you decide that all non-numeric values should be treated as 0 and cast to the .

Fix these warnings! In PHP 8, this becomes a fatal error: "Uncaught TypeError: Unsupported operand types".

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.