Warning: A non-numeric value encountered

后端 未结 18 1618
抹茶落季
抹茶落季 2020-11-22 06:15

Recently updated to PHP 7.1 and start getting following error

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

Here is wha

相关标签:
18条回答
  • 2020-11-22 06:34

    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.

    0 讨论(0)
  • If non-numeric value encountered in your code try below one. The below code is converted to float.

    $PlannedAmount = ''; // empty string ''
    
    if(!is_numeric($PlannedAmount)) {
               $PlannedAmount = floatval($PlannedAmount);
     }
    
     echo $PlannedAmount; //output = 0
    
    0 讨论(0)
  • 2020-11-22 06:35

    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:

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

    0 讨论(0)
  • 2020-11-22 06:37

    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])));
    0 讨论(0)
  • 2020-11-22 06:38

    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
    ?>
    
    0 讨论(0)
  • 2020-11-22 06:40

    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.

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

    0 讨论(0)
提交回复
热议问题