Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Two Variables Together 1

Status
Not open for further replies.

TonyRosen

Programmer
Jul 28, 2003
108
US
Sorry in advance, but I am very new to PHP ...

Okay, I have two variables:
$var1 = $_POST['form_var1'];
$var2 = pulls a number from a MySQL Database.

The variables are correct:
$var1 = 1,410.00
$var2 = 1,476.10

However, when I add them:
$var_sum = ($var1 + $var2)

My answer ALWAYS prints out as:
$var_sum = 2

Which, of course, is wrong as it SHOULD equal 2,886.10 ...

What would cause this?
 
Do the values include the commas? If so, they are not number, they are strings.

Code:
<?
$var1 = "1,410.00";
$var2 = "1,476.10";
$var3 = $var1 + $var2;
echo $var3;
?>

2

Get rid of the commas and you will get the correct result.
Code:
<?
$var1 = "1,410.00";
$var2 = "1,476.10";
$var3 = str_replace(',','',$var1) + str_replace(',','',$var2);
echo $var3;
?>

2886.1

Read up on types in the PHP Manual
Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top