Link to home
Create AccountLog in
Avatar of Neil Thompson
Neil ThompsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

cannot get $totCost = $row['totals'] * 0.09; to return a value

Hi

I have a row coming back from my database that I need to be able to multiply and write.

If I just echo $row['totals']  --------------------> I get 123

If I echo $row['totals'] * 0.09; ----> I get 0

How can I get the row value to multiply?

Neil
ASKER CERTIFIED SOLUTION
Avatar of und3ath
und3ath

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
If $row['totals'] contains a number (even in the form of a string) it should be converted to it's numeric value when multiplying. Try using these to debug the problem, let me know what the results are:
echo 'value: -'.$row['totals'].'-<br>;
echo 'float: -'.floatval($row['totals']).'-<br>;
echo 'int: -'.intval($row['totals']).'-<br>;
echo 'multiply: -'.($row['totals'] * 0.09).'-<br>';
echo $row['totals'] * 0.09;

Open in new window

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Neil Thompson

ASKER

Apologies, and thanks... it did work 100%, had a damn } hiding off the side of the page meaning I was putting the code outside the loop.

Neil
Avatar of und3ath
und3ath

thanks a lot :)
You're welcome. At least as of now you know how to debug these things. Trace what happens to the variable from it's original state until it gets converted to a numeric and becomes an operand and finally check the result. This way you can find out what happens "wrong" on it's way from being $row['totals'] to becoming a float operand.
thanks rohypnol, I always bookmark little snippits like yours, they always come in handy!
Cheers
Neil