programing tip

경고 : 숫자가 아닌 값이 발견되었습니다.

itbloger 2020. 9. 20. 09:08
반응형

경고 : 숫자가 아닌 값이 발견되었습니다.


최근 PHP 7.1로 업데이트되었으며 다음 오류가 발생하기 시작했습니다.

경고 : 29 행에 숫자가 아닌 값이 있습니다.

29 번 줄은 다음과 같습니다.

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

localhost에서 모두 잘 작동합니다 ..

이것을 해결하는 방법이나 그것이 무엇인지 어떤 아이디어?


PHP 7.1에서 숫자가 아닌 값이 발견되면 경고가 발생하는 것 같습니다. 링크를 참조하십시오 .

다음은 귀하가 받고있는 경고 통지와 관련된 관련 부분입니다.

숫자 또는 그에 상응하는 할당을 예상하는 연산자를 사용하여 유효하지 않은 문자열을 강제 할 때 새로운 E_WARNING 및 E_NOTICE 오류가 도입되었습니다. 문자열이 숫자 값으로 시작하지만 뒤에 숫자가 아닌 문자가 포함 된 경우 E_NOTICE가 발생하고 문자열에 숫자 값이 포함되지 않은 경우 E_WARNING이 발생합니다.

나도 같은데요 $ 항목 [ '수량'] 또는 $ 제품 [ '가격'] 확신 그들을 번성하기 전에 할 것을 확인, 숫자 값을 포함하지 않습니다. $ sub_total을 계산하기 전에 다음과 같이 조건부를 사용할 수 있습니다.

<?php

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

정확히 당신이 가진 문제는 아니지만 검색하는 사람들에게 동일한 오류가 있습니다.

JavaScript에 너무 많은 시간을 할애했을 때 이런 일이 발생했습니다.

PHP로 돌아와서 " +"대신 " "로 두 문자열을 연결 .하고 오류가 발생했습니다.


경고를 방지하고 PHP 7.0 이하의 동작과 동일하게 숫자로 캐스팅하여 새로운 논리없이 문제를 해결할 수 있습니다.

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

(Daniel Schroeder의 답변은 숫자가 아닌 값이 발견되면 $ sub_total이 설정되지 않은 상태로 유지되기 때문에 동일하지 않습니다. 예를 들어, $ sub_total을 인쇄하면 빈 문자열이 표시되며 이는 송장에서 잘못되었을 수 있습니다.-by 캐스팅하면 $ 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'];
}

누군가가 문제를 해결하기를 바랍니다.


내 페이지 매김 정방향 및 역방향 링크 에서이 문제가 발생했습니다 .... 변수 $ 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 }
?>

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

into

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

Fixed.


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

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.


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

}

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.

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.


Solve this error on WordPress

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

Simple solution here!

  1. locate a file of wp-includes\SimplePie\Parse\Date.php
  2. find a 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])));

$sub_total_price = 0; 

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

echo $sub_total_price;

it's working 100% :)


That's happen usually when you con-cat strings with + sign. In PHP you can make concatenation using dot sign (.) So sometimes I accidentally put + sign between two strings in PHP, and it show me this error, since you can use + sign in numbers only.


Make sure that your column structure is INT.

참고URL : https://stackoverflow.com/questions/42044127/warning-a-non-numeric-value-encountered

반응형