Click to See Complete Forum and Search --> : How to multiplication....


invader7
October 22nd, 2009, 05:32 PM
hello everybody, i have a problem and i think someone of you could solve it.

i have this line of code


$price2 =$row['price'] * $quantity;
echo $price2;


echo gives me : 200(price)3(quantity) like 2003 and not 600 as i want...

$row['price'] is from my database (varchar column, tried with int column)
and quantity is

$_SESSION["products"] as $product => $quantity

i tried these:


(int)$price2 =$row['price'] * $quantity;
echo $price2;

$price2 =(int)$row['price'] * (int)$quantity;
echo $price2;


(int)$price2 =(int)$row['price'] * (int)$quantity;
echo (int)$price2;


none worked for me... can anyone help me?

PeejAvery
October 22nd, 2009, 07:01 PM
PHP will automatically converts object type. You don't need to do that yourself. In fact, the only way it should be concatenating is if you are using a "." instead of a "*".

I guess you could try the following...
$price2 = ($row['price']) * ($quantity);

invader7
October 25th, 2009, 09:50 AM
$price2 =($row['price']) * ($quantity);
echo $price2;


and i get 700200 for example... i want 140000

PeejAvery
October 25th, 2009, 11:18 AM
Strange. :confused: PHP should always do variable type conversion. Try using intval() (http://php.net/manual/en/function.intval.php).

Ashton321
November 12th, 2009, 08:47 PM
Do your variables echo to what you expect?