faqts : Computers : Programming : Languages : PHP : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

57 of 72 people (79%) answered Yes
Recently 7 of 10 people (70%) answered Yes

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.