Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: A non-numeric value encountered

Tags:

php

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 ?

like image 623
Imran Rafique Avatar asked Feb 04 '17 18:02

Imran Rafique


People also ask

What is a non numeric value?

Nonnumeric data types are data that cannot be manipulated mathematically using. standard arithmetic operators. The non-numeric data comprises text or string data. types, the Date data types, the Boolean data types that store only two values (true or.

How do I fix non numeric value encountered in Wordpress?

solution: deactivating some plugin, theme and check if the problem is fixed. If you leave “line cost” empty, you get the “non-numeric value” error. The work around we found was to make sure all “line cost” had a value. We use 0, and that worked.


11 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.

like image 191
Yassir Ennazk Avatar answered Oct 13 '22 13:10

Yassir Ennazk


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...
}
like image 24
djs Avatar answered Oct 13 '22 15:10

djs


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.)

like image 29
Roland Seuhs Avatar answered Oct 13 '22 15:10

Roland Seuhs


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

like image 33
CodeToLife Avatar answered Oct 13 '22 13:10

CodeToLife


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 :)

like image 25
Jodyshop Avatar answered Oct 13 '22 13:10

Jodyshop


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 }
?>
like image 34
Ajmal Aamir Avatar answered Oct 13 '22 14:10

Ajmal Aamir


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

like image 35
coderama Avatar answered Oct 13 '22 13:10

coderama


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.

like image 37
Henry Avatar answered Oct 13 '22 15:10

Henry


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.

like image 31
Wadday Avatar answered Oct 13 '22 13:10

Wadday


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;
}
like image 39
Luiz Gustavo Martins Avatar answered Oct 13 '22 14:10

Luiz Gustavo Martins


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".

PHP 8

Code that used to produce the warning "A non well formed numeric value encountered" in PHP 7.1-7.4 now gives this warning instead. This happens when you have a "trailing string", which is a string that starts with a number, but is followed by something non-numeric. (It will still do the math but you should fix this! In the future it may be upgraded to an error.) For example:

echo 3 + "30 meters";

Output:

Warning: A non-numeric value encountered in [...][...] on line X
33

Fix:

echo 3 + (float) "30 meters";
like image 20
Laurel Avatar answered Oct 13 '22 13:10

Laurel