警告:遇到的非数值值

时间:2022-06-24 09:58:19

Recently updated to PHP 7.1 and start getting following error

最近更新到PHP 7.1并开始出现以下错误

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

警告:第29行中遇到的非数值

Here is what line 29 looks like

这是29号线

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

On localhost all works fine..

在本地主机上一切正常。

Any ideas how to tackle this or what it is ?

有什么办法解决这个问题吗?

12 个解决方案

#1


26  

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

在PHP 7.1中,如果遇到非数值,则会发出警告。看到这个链接。

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.

新的E_WARNING和E_NOTICE错误已经引入,当无效字符串被强制使用期望数字的操作符或它们的赋值等价符时。当字符串以数字值开始,但包含尾随的非数字字符时,会发出E_NOTICE,当字符串不包含数值时,会发出E_WARNING。

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:

我猜$item['quantity']或$product['price']都不包含数值,所以在尝试将它们相乘之前一定要确保它们包含数值。在计算$sub_total之前,可以使用某种条件,比如:

<?php

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

#2


28  

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:

你可以不需要任何新的逻辑就可以解决这个问题,只要将它插入到数字中就可以了,这样就可以防止出现警告,相当于PHP 7.0和下面的行为:

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

(Daniel Schroeder的回答并不等价,因为如果遇到非数值,$sub_total将保持未设置。例如,如果输出$sub_total,您将得到一个空字符串,这在发票中可能是错误的。-通过强制转换,您可以确保$sub_total是一个整数。

#3


23  

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.

当我花太多时间在JavaScript上的时候,我就遇到了这种情况。

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

回到PHP,我用“+”而不是“。”连接了两个字符串,得到了这个错误。

#4


2  

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

在我的例子中,是因为我在其他语言中使用了“+”,而在PHP字符串中,连接操作符是“”。。

#5


2  

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

这个问题我已经和我的分页向前和向后链接....只需在变量$Page+1前面设置(int),它就可以工作了……

<?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 }
?>

#6


1  

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.

它应该能解决你的问题。

#7


0  

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;
}

#8


0  

I just looked at this page as I had this issue. For me I had floating point numbers calculated from an array but even after designating the variables as floating points the error was still given, here's the simple fix and example code underneath which was causing the issue.

我刚看了这一页,因为我有这个问题。对我来说,我有一个从数组中计算出的浮点数,但即使将变量指定为浮点数,错误仍然被给出,下面是导致问题的简单修复和示例代码。

Example PHP

PHP示例

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

#9


0  

$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's the code I had which looped and created new html div elements. The code worked fine and the elements were formed, but i got the same warning in the error_log.

这就是我创建和创建新的html div元素的代码。代码运行良好,元素也形成了,但是我在error_log中得到了相同的警告。

After reading the useful other answers, I figured that I was summing up a string and a number in the erroneous line. So I changed the code at that line to

在阅读了其他有用的答案后,我认为我是在对错误行中的一个字符串和一个数字进行求和。我把这一行的代码改成

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

Now the code works as earlier but with no warnings this time. Hope this example would be useful to someone.

现在代码可以像以前一样工作了,但是这次没有任何警告。希望这个例子对某些人有用。

#10


0  

Solve this error on WordPress

在WordPress上解决这个错误。

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

警告:在C中遇到的非数字值:\XAMPP\htdocs\ ad-2\wp- 2\ \ \SimplePie\Parse\Date。php在第694行

Simple solution here!

简单的解决方案!

  1. locate a file of wp-includes\SimplePie\Parse\Date.php
  2. 定位一个包含wp的\SimplePie\Parse\ data .php文件
  3. find a line no. 694
  4. 找到一个行不。694年
  5. you show the code $second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
  6. 代码$second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
  7. and change this 3.) to this line $second = round((int)$match[6] + (int)$match[7] / pow(10, strlen($match[7])));
  8. 将这3.)改为这一行$second = round(int)$match[6] + (int)$match[7] / pow(10, strlen($match[7]));

#11


0  

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

这是在PHPMyAdmin上发生的。为了更具体地回答这个问题,我做了如下的事:

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

希望救救某人有些麻烦……

#12


-1  

$sub_total_price = 0; 

foreach($booking_list as $key=>$value) {
        $sub_total_price += ($price * $quantity); 
}

echo $sub_total_price;

it's working 100% :)

它的工作:100%)

#1


26  

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

在PHP 7.1中,如果遇到非数值,则会发出警告。看到这个链接。

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.

新的E_WARNING和E_NOTICE错误已经引入,当无效字符串被强制使用期望数字的操作符或它们的赋值等价符时。当字符串以数字值开始,但包含尾随的非数字字符时,会发出E_NOTICE,当字符串不包含数值时,会发出E_WARNING。

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:

我猜$item['quantity']或$product['price']都不包含数值,所以在尝试将它们相乘之前一定要确保它们包含数值。在计算$sub_total之前,可以使用某种条件,比如:

<?php

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

#2


28  

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:

你可以不需要任何新的逻辑就可以解决这个问题,只要将它插入到数字中就可以了,这样就可以防止出现警告,相当于PHP 7.0和下面的行为:

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

(Daniel Schroeder的回答并不等价,因为如果遇到非数值,$sub_total将保持未设置。例如,如果输出$sub_total,您将得到一个空字符串,这在发票中可能是错误的。-通过强制转换,您可以确保$sub_total是一个整数。

#3


23  

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.

当我花太多时间在JavaScript上的时候,我就遇到了这种情况。

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

回到PHP,我用“+”而不是“。”连接了两个字符串,得到了这个错误。

#4


2  

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

在我的例子中,是因为我在其他语言中使用了“+”,而在PHP字符串中,连接操作符是“”。。

#5


2  

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

这个问题我已经和我的分页向前和向后链接....只需在变量$Page+1前面设置(int),它就可以工作了……

<?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 }
?>

#6


1  

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.

它应该能解决你的问题。

#7


0  

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;
}

#8


0  

I just looked at this page as I had this issue. For me I had floating point numbers calculated from an array but even after designating the variables as floating points the error was still given, here's the simple fix and example code underneath which was causing the issue.

我刚看了这一页,因为我有这个问题。对我来说,我有一个从数组中计算出的浮点数,但即使将变量指定为浮点数,错误仍然被给出,下面是导致问题的简单修复和示例代码。

Example PHP

PHP示例

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

#9


0  

$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's the code I had which looped and created new html div elements. The code worked fine and the elements were formed, but i got the same warning in the error_log.

这就是我创建和创建新的html div元素的代码。代码运行良好,元素也形成了,但是我在error_log中得到了相同的警告。

After reading the useful other answers, I figured that I was summing up a string and a number in the erroneous line. So I changed the code at that line to

在阅读了其他有用的答案后,我认为我是在对错误行中的一个字符串和一个数字进行求和。我把这一行的代码改成

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

Now the code works as earlier but with no warnings this time. Hope this example would be useful to someone.

现在代码可以像以前一样工作了,但是这次没有任何警告。希望这个例子对某些人有用。

#10


0  

Solve this error on WordPress

在WordPress上解决这个错误。

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

警告:在C中遇到的非数字值:\XAMPP\htdocs\ ad-2\wp- 2\ \ \SimplePie\Parse\Date。php在第694行

Simple solution here!

简单的解决方案!

  1. locate a file of wp-includes\SimplePie\Parse\Date.php
  2. 定位一个包含wp的\SimplePie\Parse\ data .php文件
  3. find a line no. 694
  4. 找到一个行不。694年
  5. you show the code $second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
  6. 代码$second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
  7. and change this 3.) to this line $second = round((int)$match[6] + (int)$match[7] / pow(10, strlen($match[7])));
  8. 将这3.)改为这一行$second = round(int)$match[6] + (int)$match[7] / pow(10, strlen($match[7]));

#11


0  

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

这是在PHPMyAdmin上发生的。为了更具体地回答这个问题,我做了如下的事:

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

希望救救某人有些麻烦……

#12


-1  

$sub_total_price = 0; 

foreach($booking_list as $key=>$value) {
        $sub_total_price += ($price * $quantity); 
}

echo $sub_total_price;

it's working 100% :)

它的工作:100%)