A non-numeric value encountered PHP エラー
php5.2からphp7に変更対応したときにでてきたエラー
https://secure.php.net/manual/ja/migration71.other-changes.php
数値形式ではない文字列を使って、数値を期待する演算 (+ - * / ** % << >> | & ^ や、これらを用いた代入演算) を行おうとしたときに、 E_WARNING あるいは E_NOTICE レベルのエラーが発生するようになりました。 E_NOTICE が発生するのは、文字列が数値で始まっていながら非数値で終わる場合で、 E_WARNING が発生するのは文字列中に数値が含まれない場合です。
参考コード
// カート内商品数の合計
function getTotalQuantity($shop_id) {
$total = 0;
$max = $this->getMax($shop_id);
for($i = 0; $i <= $max; $i++) {
$total+= $this->cartSession[$shop_id][$i]['quantity'];
}
return $total;
}
この場合、以下のように型が違うためWarningが出てしまう。
変数 | 型 |
---|---|
$total | int |
$this->cartSession[$shop_id][$i]['quantity'] | string |
以下の様にint型で揃えるとエラーがでない
$total+= $this->cartSession[$shop_id][$i]['quantity'];
↓
$total+= (int)$this->cartSession[$shop_id][$i]['quantity'];