I received this error after adding two formatted variables using the number_format() method. The reason is because this method usually formats your number with a ,
unless you specified the optional separator argument as null
. For example, let's say I have the following code:
$subtotal = 6556.7;
$vat = number_format(0.175 * $subtotal, 2); // 1,147.42
$transactionfee = number_format(0.02 * $subtotal, 2); // 131.13
$total = $subtotal + $vat + $transactionfee; // 6556.7 + 1,147.42 + 131.13 = ERROR
This would trigger the following error because one of the variables contains a ,
which is invalid for a float type:
Warning: A non-numeric value encountered ...
The number_format()
method has the following method signature:
number_format(number,decimals,decimalpoint,separator)
.
By specifying the separator argument as ''
, this error could be fixed as follows:
$subtotal = 6556.7;
$vat = number_format(0.175 * $subtotal, 2, '.', ''); //1147.42
$transactionfee = number_format(0.02 * $subtotal, 2, '.', ''); //131.13
$total = $subtotal + $vat + $transactionfee; // // 6556.7 + 1147.42 + 131.13 = 7835.25
var_dump($item['quantity'], $product['price'])