最近更新到PHP 7.1,并开始出现以下错误
警告:第29行遇到非数值
这是第29行的样子
$sub_total += ($item['quantity'] * $product['price']);
在本地主机上一切正常。
任何想法如何解决这个问题或它是什么?
最近更新到PHP 7.1,并开始出现以下错误
警告:第29行遇到非数值
这是第29行的样子
$sub_total += ($item['quantity'] * $product['price']);
在本地主机上一切正常。
任何想法如何解决这个问题或它是什么?
Answers:
似乎在PHP 7.1中,如果遇到非数字值,将发出警告。请参阅此链接。
这是与您收到的警告通知有关的相关部分:
当使用期望数字或其赋值等效项的运算符强制无效字符串时,将引入新的E_WARNING和E_NOTICE错误。当字符串以数字开头但包含尾随非数字字符时,将发出E_NOTICE;而 当字符串不包含数字值时,将发出E_WARNING。
我猜$ 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...
}
$sub_total += ((int)$item['quantity'] * (int)$product['price']);
与您遇到的问题不完全相同,但是对于搜索对象来说却存在相同的错误。
当我花太多时间在JavaScript上时,这发生在我身上。
回到PHP,我用“ +
”而不是“ .
” 连接了两个字符串,并得到了该错误。
+
,就假定它是一个int
值...而给定的操作数则不是任何数字数据类型。
+
不是.
。谢谢!
您可以通过将事物强制转换为数字来解决问题,而无需任何新逻辑,这可以防止出现警告,并且等同于PHP 7.0及以下版本中的行为:
$sub_total += ((int)$item['quantity'] * (int)$product['price']);
(Daniel Schroeder的回答并不相同,因为如果遇到非数字值,$ sub_total将保持不变。例如,如果打印出$ sub_total,您将得到一个空字符串,这在发票中可能是错误的。强制转换,请确保$ sub_total是整数。)
就我而言,这是因为与+
其他语言一样使用了我,但是在PHP字符串中,串联运算符是.
。
这是发生在我身上的,特别是在PHPMyAdmin上。因此,为了更具体地回答这个问题,我做了以下工作:
在文件中:
C:\ampps\phpMyAdmin\libraries\DisplayResults.class.php
我改变了这个:
// Move to the next page or to the last one
$endpos = $_SESSION['tmpval']['pos']
+ $_SESSION['tmpval']['max_rows'];
对此:
$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'];
}
希望能给别人省些麻烦...
我在PHP 7.3的phpmyadmin中遇到了这个问题。谢谢@coderama,我将libraries / DisplayResults.class.php第855行从
// Move to the next page or to the last one
$endpos = $_SESSION['tmpval']['pos']
+ $_SESSION['tmpval']['max_rows'];
进入
// Move to the next page or to the last one
$endpos = (int)$_SESSION['tmpval']['pos']
+ (int)$_SESSION['tmpval']['max_rows'];
固定。
检查您是否没有使用某个变量递增其值是一个空字符串,如“”。
例:
$total = '';
$integers = range(1, 5);
foreach($integers as $integer) {
$total += $integer;
}
遇到此问题时,我只是看着此页面。对我来说,我有一个从数组计算出的浮点数,但是即使将变量指定为浮点后,仍然会给出错误,这是导致问题的简单的修复和示例代码。
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
?>
$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
}
这就是我循环并创建新的html div元素的代码。代码工作正常,元素形成,但在error_log中得到了相同的警告。
在阅读了其他有用的答案之后,我发现我在错误的行中总结了一个字符串和一个数字。所以我将那一行的代码更改为
/*erroneous line*/$job_no = "job"&&$sn;//this is the new variable that will create the job rows
现在,代码可以像以前那样工作,但是这次没有警告。希望这个例子对某人有用。
解决WordPress上的此错误
警告:第694行的C:\ XAMPP \ htdocs \ aad-2 \ wp-includes \ SimplePie \ Parse \ Date.php中遇到的非数字值
简单的解决方案在这里!
wp-includes\SimplePie\Parse\Date.php
$second = round($match[6] + $match[7] / pow(10, strlen($match[7])));
$second = round((int)$match[6] + (int)$match[7] / pow(10, strlen($match[7])));
$sub_total_price = 0;
foreach($booking_list as $key=>$value) {
$sub_total_price += ($price * $quantity);
}
echo $sub_total_price;
它正在工作100%:)
如果您的代码中遇到非数字值,请尝试以下一个。下面的代码转换为float。
$PlannedAmount = ''; // empty string ''
if(!is_numeric($PlannedAmount)) {
$PlannedAmount = floatval($PlannedAmount);
}
echo $PlannedAmount; //output = 0
在PHP中,如果您使用+进行串联,则会出现此错误。在php +中是算术运算符。 https://www.php.net/manual/zh/language.operators.arithmetic.php
+运算符的错误使用:
"<label for='content'>Content:</label>"+
"<textarea class='form-control col-xs-12' rows='7'cols='100' id='content' name='content'>"+$initcontent+"</textarea>'"+
"</div>";
使用。串联
$output = "<div class='from-group'>".
"<label for='content'>Content:</label>".
"<textarea class='form-control col-xs-12' rows='7'cols='100' id='content' name='content'>".$initcontent."</textarea>'".
"</div>";
var_dump($item['quantity'], $product['price'])