php 警告:遇到非数字值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42044127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:32:12  来源:igfitidea点击:

Warning: A non-numeric value encountered

php

提问by Imran Rafique

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 ?

任何想法如何解决这个问题或它是什么?

采纳答案by DanielSchroederDev

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

回答by Yassir Ennazk

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 我用“ +”而不是“ .”连接了两个字符串并得到了那个错误。

回答by Roland Seuhs

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,您将得到一个空字符串,这在发票中可能是错误的。- by铸造你确保 $sub_total 是一个整数。)

回答by CodeToLife

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

就我而言,这是因为我在其他语言中使用了“+”,但在 PHP 字符串中,连接运算符是“。” .

回答by coderama

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

希望救人一些麻烦...

回答by Ajmal Aamir

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

回答by Henry

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

我在使用 PHP 7.3 的 phpmyadmin 中遇到了这个问题。谢谢@coderama,我从

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

固定的。

回答by Luiz Gustavo Martins

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

回答by Luiz Gustavo Martins

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

回答by Neo

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

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