I am trying to give the money format 2 variables but I am getting an error message which is this post topic

I did it like

$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
$minicartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_GB.UTF-8");
$pricetotal = money_format("%n", $pricetotal);

I am getting the error message on this codes

setlocale(LC_MONETARY, "en_GB.UTF-8");
$_SESSION ['minicart'] = $minicartTotal = money_format("%n", $cartTotal);
$cartTotal = money_format("%n", $cartTotal);

also u can see $minicartTotal and $cartTotal are both doing the same thing... but they i have done it is that i gave it a different variable number but the contents is the same.

What is the best way of doing this to stop the errors

有帮助吗?

解决方案

The easiest way is just to typecast the number like this:

$_SESSION['minicart'] = $minicartTotal = money_format("%n", (double)$cartTotal);

But the best way is to make sure that $price is a double and $each_item['quantity'] is a integer because they are the only unknown variables in your example and if they aren't those types you will get that error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top