Entry
How can I print a dollar sign in a string?
How can I format currency properly in PHP?
Nov 2nd, 2000 09:32
David Croft, Nathan Wallace, Alfred Perlstein
Printing dollar signs ($) in PHP can be difficult as they are used to
denote variables. In a double quoted string (where variables are
expanded) you need to escape the dollar sign as \$ like this:
$money = sprintf ("%01.2f", $bal_fwd);
$statement .= "\nBalance Brought Forward: \$$money\n";
Or, you can use single quoted string concatenated with the variable:
$statement .= "\n".'Balance Brought Forward: $'.$money."\n";
Note that the \n newlines also have to be in double quoted strings to be
expanded.
Regarding formatting the currency number, see the number_format
function.