Coder Perfect

Warning: A non-numeric value encountered

Problem

I just upgraded to PHP 7.1 and now I’m receiving the following issue.

This is how line 29 appears.

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

Everything works well on localhost.

Do you have any suggestions how to deal with it or what it is?

Asked by Imran Rafique

Solution #1

It’s not the same issue you faced, but it’s the same mistake that people are seeing when they search.

When I spent too much time on JavaScript, this occurred to me.

Returning to PHP, I attempted to concatenate two strings using + instead of. and received the error.

Answered by Yassir Ennazk

Solution #2

If a non-numeric value is detected with PHP 7.1, it appears that a Warning will be issued. Take a look at this link.

Here’s what you need to know about the Warning notification you’re getting.

I’m guessing that neither $item[‘quantity’] nor $product[‘price’] include a numeric value, so double-check before multiplying them. Before computing the $sub total, you may apply a conditional like this:

<?php

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

Answered by djs

Solution #3

You can avoid the warning by just casting the thing into the number, which is comparable to the behaviour in PHP 7.0 and below:

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

(The response from Daniel Schroeder is not equal since non-numeric values would leave $sub total unset.) If you print out $sub total, for example, you’ll receive an empty string, which is probably incorrect in an invoice. – You ensure that $sub total is an integer by casting.)

Answered by Roland Seuhs

Solution #4

In my situation, it was because I used + as in other languages, but the concatenation operator in PHP strings is.

Answered by CodeToLife

Solution #5

Hello, In my instance, I get a warning about a numeric value issue when using (WordPress) with PHP7.4. As a result, I made the following changes to the original code:

From:

$oldval + $val; $val = $oldval + $val;

To:

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

The warning has now vanished:)

Answered by Jodyshop

Post is based on https://stackoverflow.com/questions/42044127/warning-a-non-numeric-value-encountered