Hi all,

I am doing a Conversion rate program. The outcome is always O instead of the rate. I have arrays... I think the problem is in the echo line with my variables. can someon take a look and let me know?

code is

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <title>Exchange Calculator</title>
</head>
<body>

<?PHP
$currency = array (
    "US $",
    "Yen",
    "Euro",
    "Pound",
    "Canadian $",
    "Swiss Franc",
    );

$exchangeRates = array (
    array (1, 104.61, 0.7476, 0.5198, 1.2013, 1,1573),
    array (0.009559, 1, 0.007146, 0.004969, 0.011484, 0.011063),
    array (1.3377, 139.9368, 1, 0.6953, 1.6070, 1.5481),
    array (1.9239, 201.2592, 1.4382, 1, 2.3112, 2.2265),
    array (0.8324, 87.0807, 0.6223, 0.4327, 1, 0.9634),
    array (0.8641, 90.3914, 0.6459, 0.4491, 1.0380, 1)
    );

$result = $_REQUEST{"amount"} * $exchangeRates{$_REQUEST("$currencyA")}
{$_REQUEST("$currencyB")}
$currentcurrency = ["$currencyA"] [$currencyB];
echo  "<h3>The Currency conversion is " ";
echo  "$currentcurrency</h3>";


</body>
</html>

Edited 3 Years Ago by Dani: Formatting fixed

Hi there

1. here :

array (
    "US \$", //you have to insert a slash before $ as $ is a reserved symbol. Also - better use the short names for currencies - like CAD, USD, EUR
    "Yen",
    "Euro",
    "Pound",
    "Canadian \$",
    "Swiss Franc",
    );

2. Here are most of your problems:

$result = $_REQUEST{"amount"} * $exchangeRates{$_REQUEST("$currencyA")}
{$_REQUEST("$currencyB")}
$currentcurrency = ["$currencyA"] [$currencyB];
echo  "<h3>The Currency conversion is " ";
echo  "$currentcurrency</h3>";

1. First of all, why do you use the $_REQUEST variable. Better use $_POST or $_GET variables
2. Array indexes are accessed like this $arrayVariable. In your case - in order to get the currency name from the $currency array, you have to use it like this

echo $currency[0];

in order to get 'US $' as a result on screen. Mind that you have to use square brackets like this [] not like this {} that you have used.
The curly brackets are used when defining a function like if (){ .....} else{......}

Explain what you are trying to do with this code - $currentcurrency = ["$currencyA"] [$currencyB];

And finally - to echo the result you have 2 ways to do it - using quotes like this " " or using single quote like this ' '.
Using the double quotes, php validates the code between the quotes, so if there is a variable mentioned there, it prints out the value of the variable.
so
your echo should look like this if you're using the double quotes:

echo "<h3>The Currency conversion is $currentcurrency</h3>";
This article has been dead for over six months. Start a new discussion instead.