PHP String to Float

John picture John · Jan 26, 2009 · Viewed 379.2k times · Source

I am not familiar with PHP at all and had a quick question.

I have 2 variables pricePerUnit and InvoicedUnits. Here's the code that is setting these to values:

$InvoicedUnits = ((string) $InvoiceLineItem->InvoicedUnits);
$pricePerUnit = ((string) $InvoiceLineItem->PricePerUnit);

If I output this, I get the correct values. Lets say 5000 invoiced units and 1.00 for price.

Now, I need to show the total amount spent. When I multiply these two together it doesn't work (as expected, these are strings).

But I have no clue how to parse/cast/convert variables in PHP.

What should I do?

Answer

Sampson picture Sampson · Jan 26, 2009
$rootbeer = (float) $InvoicedUnits;

Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.