【问题标题】:PHP math - operatorsPHP 数学 - 运算符
【发布时间】:2011-11-30 20:22:20
【问题描述】:

所以我有一个简单的网站,它使用 php 来做一些简单的数学运算:

<form action="" method="POST">
<input type="text" name="first" />
<select name="method">
    <option>+</option>
    <option>_</option>
    <option>*</option>
    <option>/</option>
</select>
<input type="text" name="second" />
<input type="submit" value="Equals" />

该网站让用户输入两个数字,然后选择一个数学运算符来操作它们。因此,例如在第一个输入中,用户可以输入数字 3,选择减法运算符,在下一个字段中输入数字 1 并提交。我这样处理信息:

if (isset($_POST['first']) && isset($_POST['second'])) {
   $first = $_POST['first'];
   $second = $_POST['second'];
   $method = $_POST['method'];
} 

但是,我想在提交后在网页上回显数学问题的结果。我该怎么做?只是回显所有变量只会给我(在这种情况下)“3-1”而不是“2”的实际答案。

知道如何做到这一点吗?

【问题讨论】:

标签: php math


【解决方案1】:
if (isset($_POST['first']) && isset($_POST['second'])) {
    $first = $_POST['first'];
    $second = $_POST['second'];
    $method = $_POST['method'];
    switch($method)
    {
        case '+':
            $result = $first + $second;
            break;
        case '-':
            $result = $first - $second;
            break;
        case '*':
            $result = $first * $second;
            break;
        case '/':
            // check for division by 0 (wouldn't want to blow anything up)
            $result = $first / $second;
            break;
        default:
            $result = 'undefined operation';
            break;
    }
    printf("%.5f %s %.5f = %s", $first, $method, $second, $result);
}

【讨论】:

  • $second 是一个整数,因此您可能希望使用%d 而不是第二个%s
  • @Arjan:你怎么知道它是一个整数?他没有说这些值是整数。我可能会在两个参数中添加检查 is_numeric
  • 在 Tims 代码的第 2 行和第 3 行中,数字输入被转换为整数。
【解决方案2】:

您需要在 $method 上使用 switch 语句,如下所示:

switch($method){

    case '+':
        $result = $first + $second;
        break;

    case '-':
        $result = $first - $second;
        break;

    case '*':
        $result = $first * $second;
        break;

    case '/':
        $result = $first / $second;
        break;

}
var_dump($result);

或者,您也可以 EVAL 操作:

$code = '$result = '.$first.$method.$second.';';
exec($code);
var_dump($result);

请注意,您不要检查您的输入,这可能会导致安全问题:

if(!isset($_POST['first']) || !is_numeric($_POST['first'])){ exit('Bad number in #1'); }

对其他输入,例如第一个、第二个和方法执行相同的操作:)

【讨论】:

    【解决方案3】:

    您可以使用开关盒。

    switch($method){
    
    case "+":
        $result = $first + $second;
        break;
    case "-":
        $result = $first - $second;
        break;
    case "*":
        $result = $first * $second;
        break;
    case "/":
        $result = $first/$second;
        break;
    }
    
    echo $result;
    

    【讨论】:

    • 请格式化您的答案?对于大多数会查看您的答案的人来说,这真的很难阅读
    • 我早点提交我完成了
    【解决方案4】:

    只评估表达式是很危险的,因为它可能包含讨厌的东西,而且您永远不应该相信用户输入(即使您使用的是 select 标记,有人可以注入任何东西)。

    要么先清理输入然后评估它(例如,检查两个数字实际上是数字,并且该方法只是可接受列表中的一个),或者在方法变量上使用switch 语句并将每个方法写出PHP。

    您也可以查看 PHP 中的脚本语言,例如 LUA。

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多