使用PHP的货币转换器

use*_*751 1 php currency converter calculator

我是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>
Run Code Online (Sandbox Code Playgroud)

Sha*_*tta 7

由于您是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;
} 
?>
Run Code Online (Sandbox Code Playgroud)

快乐的编码!