Warning: non-numeric value encountered

Recently upgraded to PHP 7.1 and will start to receive the following error

Warning. Non-numeric value found on line 29

Here is line 29

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

Everything works fine on the local host.

Any ideas how to solve this or what is it?

+75
php
Feb 04 '17 at 18:42 on
source share
15 answers

It looks like in PHP 7.1 a warning will be issued if a non-numeric value is encountered. See Link.

Here is the relevant part regarding the alert notification you receive:

New errors E_WARNING and E_NOTICE were introduced if they are invalid lines are forcibly used by operators waiting for numbers or their equivalent assignment. E_NOTICE is emitted when a string begins with a numeric value, but contains trailing non-numeric characters, and E_WARNING is emitted when a string does not contain a numeric value.

I assume that $ item ['quantity'] or $ product ['price'] does not contain a numeric value, so make sure they do before trying to multiply them. Perhaps use some conditional signal before computing $ sub_total, for example:

 <?php if (is_numeric($item['quantity']) && is_numeric($product['price'])) { $sub_total += ($item['quantity'] * $product['price']); } else { // do some error handling... } 
+67
Feb 04 '17 at 19:07
source share

Not exactly the problem you had, but the same error for people who are looking.

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

Returning to PHP, I linked the two lines with a β€œ + ” instead of a β€œ . ” And I got this error.

+115
May 19 '17 at 9:58 a.m.
source share

You can solve the problem without any new logic by simply entering a thing into a number, which prevents a warning and is equivalent to 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 will remain undefined if non-numeric values ​​are encountered. For example, if you print $ sub_total, you will get an empty string, which is probably incorrect in the invoice. - by casting you will make sure that $ sub_total is an integer.)

+43
May 12 '17 at 7:11
source share

In my case, it was due to the fact that I used "+", as in other languages, but in PHP the concatenation string is "." ,

+12
Feb 24 '18 at 10:25
source share

This happened to me specifically on PHPMyAdmin. To answer this more specifically, 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 that:

 $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 this saves someone from the trouble ...

+5
Feb 27 '18 at 5:15
source share

I had this problem with my pagination of the forward and backward links .... just set (int) before 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 } ?> 
+3
Apr 28 '18 at 16:29
source share

Check if you are incrementing a variable that its value is an empty string of type ''.

Example:

 $total = ''; $integers = range(1, 5); foreach($integers as $integer) { $total += $integer; } 
+2
Nov 21 '17 at 20:27
source share

I ran into a problem in phpmyadmin with PHP 7.3. Thanks @coderama, I changed the 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']; 

at

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

Fixed.

+2
Nov 24 '18 at 17:40
source share

Try it.

 $sub_total = 0; 

and in your loop now you can use this

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

It should solve your problem.

+1
May 11 '17 at 10:09 PM
source share

I just looked at this page as I had this problem. For me, I had floating point numbers calculated from an array, but even after the variables were designated as floating points, an error was still indicated, here is a simple error code and an example under which the problem arose.

PHP example

 <?php $subtotal = 0; //Warning fixed $shippingtotal = 0; //Warning fixed $price = array($row3['price']); $shipping = array($row3['shipping']); $values1 = array_sum($price); $values2 = array_sum($shipping); (float)$subtotal += $values1; // float is irrelevant $subtotal creates warning (float)$shippingtotal += $values2; // float is irrelevant $shippingtotal creates warning ?> 
+1
Nov 28 '17 at 0:05
source share
 $sn = 0;//increment the serial number, then add the sn to job for($x = 0; $x<20; $x++) { $sn++; $added_date = "10/10/10"; $job_title = "new job"; $salary = $sn*1000; $cd = "27/10/2017";//the closing date $ins = "some institution";//the institution for the vacancy $notes = "some notes here";//any notes about the jobs $sn_div = "<div class='sn_div'>".$sn."</div>"; $ad_div = "<div class='ad_div'>".$added_date."</div>"; $job_div = "<div class='job_div'>".$job_title."</div>"; $salary_div = "<div class='salary_div'>".$salary."</div>"; $cd_div = "<div class='cd_div'>".$cd."</div>";//cd means closing date $ins_div = "<div class='ins_div'>".$ins."</div>";//ins means institution $notes_div = "<div class='notes_div'>".$notes."</div>"; /*erroneous line*/$job_no = "job"+$sn;//to create the job rows $$job_no = "<div class='job_wrapper'>".$sn_div.$ad_div.$job_div.$salary_div.$cd_div.$ins_div.$notes_div."</div>"; echo $$job_no;//and then echo each job } 

that I had code that looped and created new html div elements. The code worked fine and the elements were generated, but I got the same warning in error_log.

After reading other useful answers, I thought I was summing up the string and the number in the wrong string. So I changed the code on this line to

 /*erroneous line*/$job_no = "job"&&$sn;//this is the new variable that will create the job rows 

Now the code works as before, but without warnings this time. Hope this example is helpful to someone.

+1
Nov 28 '17 at 3:00
source share

Solve this error on WordPress

Warning: non-numeric value found in C: \ XAMPP \ htdocs \ aad-2 \ wp-includes \ SimplePie \ Parse \ Date.php on line 694

The simple solution is here!

  1. find the file wp-includes\SimplePie\Parse\Date.php
  2. find the line no. 694
  3. you show the code $second = round($match[6] + $match[7]/pow(10, strlen($match[7])));
  4. and change this 3.) to this line $second = round((int)$match[6] + (int)$match[7]/pow(10, strlen($match[7])));
+1
Feb 16 '18 at 13:37
source share
 $sub_total_price = 0; foreach($booking_list as $key=>$value) { $sub_total_price += ($price * $quantity); } echo $sub_total_price; 

it works 100% :)

+1
Apr 24 '18 at 7:01
source share

This usually happens when you join lines with a + sign. In PHP you can concatenate using the dot sign (.) So sometimes I accidentally put a + sign between two lines in PHP, and this shows me this error, since you can only use the + sign in numbers.

+1
Jul 22 '18 at
source share

Make sure your column structure is INT.

0
Jul 15 '19 at 5:55
source share



All Articles