【问题标题】:Currency Converter Using PHP使用 PHP 的货币转换器
【发布时间】:2014-04-08 08:56:28
【问题描述】:

我是 PHP 新手,所以我不确定我做错了什么,但无论我选择什么货币,如果语句被执行,它似乎只是最后一个,即美元对美元。

我需要让它转换我选择的 FROM 和 TO 货币,并且转换显示在 amount_output 框中。而且我输入的货币和坐骑会保留在结果中而不会被重置。

非常感谢任何帮助。

<?php                
$amount_input = filter_input(INPUT_POST, 'amount_input');
$currency1 = filter_input(INPUT_POST, 'currency1');
$currency2 = filter_input(INPUT_POST, 'currency2');

if ($currency1="CAD" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.0;
}

if ($currency1="CAD" AND $currency2="EUR")
{
    $amount_output = $amount_input*0.624514066;
}       

if ($currency1="CAD" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.588714763;
}       

if ($currency1="CAD" AND $currency2="USD")
{
    $amount_output = $amount_input*0.810307;
}       
if ($currency1="EUR" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.601244959;
}

if ($currency1="EUR" AND $currency2="EUR")
{
    $amount_output = $amount_input*1.0;
}       

if ($currency1="EUR" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.942676548;
}       

if ($currency1="EUR" AND $currency2="USD")
{
    $amount_output = $amount_input*1.2975;
}

if ($currency1="GBP" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.698615463;
}

if ($currency1="GBP" AND $currency2="EUR")
{
    $amount_output = $amount_input*1.060809248;
}       

if ($currency1="GBP" AND $currency2="GBP")
{
    $amount_output = $amount_input*1.0;
}       

if ($currency1="GBP" AND $currency2="USD")
{
    $amount_output = $amount_input*1.3764;
} 

if ($currency1="USD" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.234100162;
}

if ($currency1="USD" AND $currency2="EUR")
{
    $amount_output = $amount_input*0.772200772;
}       

if ($currency1="USD" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.726532984;
}       

if ($currency1="USD" AND $currency2="USD")
{
    $amount_output = $amount_input*1.0;
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>F/X Calculator</title>
    <link href="fxCalcStyles.css" rel="stylesheet" type="text/css" />

</head>
<body>        
    <h1>Money Banks F/X Calculator</h1>
    <hr></hr>
    <form action="fxCalc.php" method="post">                
        <select name="currency1">
            <option value="CAD">CAD</option>
            <option value="EUR">EUR</option>
            <option value="GBP">GBP</option>
            <option value="USD">USD</option>
        </select>
        <input name="amount_input" type="text"/>
        <select name="currency2">
            <option value="CAD">CAD</option>
            <option value="EUR">EUR</option>
            <option value="GBP">GBP</option>
            <option value="USD">USD</option>
        </select>
        <input name="amount_output" type="text" value="<?php echo $amount_output ?>" readonly="readonly"/>

        <br />

        <input type="submit" name="submit" value="Convert"></input>
        <input type="reset" name="reset" value="Reset"></input>

    </form>                        
</body>
</html>

【问题讨论】:

  • 使用二维的转化率数组会容易得多
  • 或将所有货币基于美元放在一个数组中

标签: php converter calculator currency


【解决方案1】:

由于您是 PHP 新手,因此我不想向您展示货币转换器的复杂方法。现在你需要学习基础知识,而不应该专注于使用高级策略使代码简洁。您的代码很不错,如果进行两次调整,它将完美运行:

  1. 比较两个值时,请使用==(比较运算符)而不是=(赋值运算符)。
  2. 使用if/else if/else 结构而不是一系列不连贯的if 结构。

现在你的代码变成了:

<?php
$amount_input = filter_input(INPUT_POST, 'amount_input');
$currency1 = filter_input(INPUT_POST, 'currency1');
$currency2 = filter_input(INPUT_POST, 'currency2');

if ($currency1 == "CAD" AND $currency2 == "CAD")
{
    $amount_output = $amount_input*1.0;
}

else if ($currency1 == "CAD" AND $currency2 == "EUR")
{
    $amount_output = $amount_input*0.624514066;
}       

else if ($currency1 == "CAD" AND $currency2 == "GBP")
{
    $amount_output = $amount_input*0.588714763;
}       

else if ($currency1 == "CAD" AND $currency2 == "USD")
{
    $amount_output = $amount_input*0.810307;
}       

else if ($currency1 == "EUR" AND $currency2 == "CAD")
{
    $amount_output = $amount_input*1.601244959;
}

else if ($currency1 == "EUR" AND $currency2 == "EUR")
{
    $amount_output = $amount_input*1.0;
}       

else if ($currency1 == "EUR" AND $currency2 == "GBP")
{
    $amount_output = $amount_input*0.942676548;
}       

else if ($currency1 == "EUR" AND $currency2 == "USD")
{
    $amount_output = $amount_input*1.2975;
}

else if ($currency1 == "GBP" AND $currency2 == "CAD")
{
    $amount_output = $amount_input*1.698615463;
}

else if ($currency1 == "GBP" AND $currency2 == "EUR")
{
    $amount_output = $amount_input*1.060809248;
}       

else if ($currency1 == "GBP" AND $currency2 == "GBP")
{
    $amount_output = $amount_input*1.0;
}       

else if ($currency1 == "GBP" AND $currency2 == "USD")
{
    $amount_output = $amount_input*1.3764;
} 

else if ($currency1 == "USD" AND $currency2 == "CAD")
{
    $amount_output = $amount_input*1.234100162;
}

else if ($currency1 == "USD" AND $currency2 == "EUR")
{
    $amount_output = $amount_input*0.772200772;
}       

else if ($currency1 == "USD" AND $currency2 == "GBP")
{
    $amount_output = $amount_input*0.726532984;
}       

else if ($currency1 == "USD" AND $currency2 == "USD")
{
    $amount_output = $amount_input*1.0;
} 
?>

编码愉快!

【讨论】:

  • 亲爱的投票者,我可以知道我的答案有什么问题吗?
【解决方案2】:

使用数组可以让您的脚本看起来更简单,而且您不会遇到这么多指令的问题:

<?php                
                $amount_input = filter_input(INPUT_POST, 'amount_input');
                $currency1 = filter_input(INPUT_POST, 'currency1');
                $currency2 = filter_input(INPUT_POST, 'currency2');

                $currencies = array();
                $currencies['CAD'] = array(
                    'CAD' => 1,
                    'EUR' => 0.624514066,
                    'GBP' => 0.588714763,
                    'USD' => 0.810307
                );

                $amount_output = $amount_input*$currencies[$currency1][$currency2];

?> 

【讨论】:

    【解决方案3】:

    这会更容易维护,所有货币都以美元为基础,然后执行以下操作:

    $currency1    = filter_input(INPUT_POST, 'currency1');
    $currency2    = filter_input(INPUT_POST, 'currency2');
    $amount_input = filter_input(INPUT_POST, 'amount_input');
    
    $currencies = array(
        'USD' => 1,
        'CAD' => 1.234100162,
        'EUR' => 0.772200772,
        'GDP' => 0.726532984
    );
    
    $amount_output = ($amount_input/$currencies[$currency1])*$currencies[$currency2];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多