Warning: A non-numeric value encountered

后端 未结 18 1831
抹茶落季
抹茶落季 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: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.)

提交回复
热议问题