2017-01-20 3 views
0

Ich bin neu in PHP und ich habe ein Problem mit meinem PHP str_replace. Ich möchte alle ersetzen. mit, aber es wird nicht funktionieren.PHP str_replace Variable Ausgabe

Dies ist der PHP-Code:

$price = 2.5; 
$row['quantity'] = 3; 
$totalprice = $price*$row['quantity']; 
$total = str_replace(".",",",$totalprice); 
echo number_format((float)$total, 2, '.', ''); 

Weiß jemand, was ich falsch gemacht?

+2

funktioniert gut für mich: https://3v4l.org/TbivE – Rizier123

+0

Hier ist alles in Ordnung. –

+0

Nach dem Editieren - verwenden Sie nicht den '$ totalprice' in' str_replace' - was erwarten Sie? – Dekel

Antwort

0

Try this:

Diese Ausgänge 7,00

$price = 2.5; 
$row['quantity'] = 3; 
$totalprice = $price*$row['quantity']; 
$total = str_replace(".",",",$totalprice); 
echo number_format($total, 2, ',', ''); 

Wenn die Ausgabe 7,50 sein soll, als Sie dies tun können:

$price = 2.5; 
$row['quantity'] = 3; 
$totalprice = $price*$row['quantity']; 
echo number_format($totalprice, 2, ',', ''); 
+0

es wird nicht funktionieren, vielleicht weil ich ein 'echo number_format ((float) $ total, 2, '.', ''); 'echo – AMG

+0

Was sind die Werte in $ Preis und $ Zeile? Bearbeiten Sie Ihre Anwsser mit dieser Information bitte. – MONZTAAA

+0

2,5 für den $ Preis und 3 für die $ Zeile ['Menge'] also dann Gesamtpreis ist 7,5 – AMG