[thelist] New to PHP - Code for calculations

Simon Willison cs1spw at bath.ac.uk
Mon Aug 11 16:36:57 CDT 2003

Hi Ken,

Monday, August 11, 2003, 10:26:19 PM, you wrote:
> Please help a PHP newbie. I don't have time to learn PHP but need to create
> a currency converter. I'd like to keep the calculation separate from the
> conversion rate, so that someone who is not web-savvy at all can go and edit
> the conversion value (variable in a file called conversion.php for example)
> and have it updated globally on a website. Only one conversion is required
> (i.e. from Canadian dollars to American).

define('CONVERSION_RATE', 1.12); // The conversion rate

if (isset($_POST['amount'])) {
    $amount = (float)$_POST['amount'];
    $converted = $amount * CONVERSION_RATE;
    $converted = number_format($converted, 2);
    $amount = number_format($amount, 2);
    print '<p>$'.$amount.' canadian dollars is $'.$converted.' US dollars</p>';
<form action="index.php" method="post">
<label>Dollars to convert: $<input type="text" name="amount"></label>
<input type="submit">

Set the conversion rate of the above, save it on a PHP enabled web
server as index.php and off you go.




More information about the thelist mailing list