I have to program a currency converter in php, which converts the following currencys:
EUR -> USD
EUR -> GBP
The code must have a good structure and clear. My code looks like this:
In index.php, I write the html inputs as follow:
<select name="fT">
<option value="euro-usd@EUR-USD">EUR -> USD</option>
<option value="euro-gbp@EUR-GBP">EUR -> GBP</option>
<select>
<input type="text" name="amount" id="amount" value="">
<input type="submit"/>
convert.php looks like this:
<?php
include ("functions.php");
$fT = $_POST['fT'];
$amount = $_POST['amount'];
$valueAndText = explode('@', $fromTo);
$fromTo = $valueAndText[0];
$text = explode('-', $valueAndText[1]);
$from = $text[0];
$to = $text[1];
echo convertCurr($fT, $amount) ;
?>
Any suggestion for improving this code?
You can place the share prices in a config file that you can replace with each change. I am considering that you do want to store the data in files and not in a DB.
The config file can look like:
Name this file as config.php,and place it with you scripts then include it.
Regarding the $_POST variables you need to escape them to protect yourself from MySQL and XSS attacks.
You can use this function:
Then loop through your POST variables:
Once more thing is the amount of processing you perform to detect the selection for the $fromto variable.
Why not to do it like that:
Then perform a Switch-Case statement
Hope that helps.