一个非常简单的 php 计算器,不会执行数学运算:(

a very simple php calculator that would not perform math operation :(

我正在尝试创建一个简单的计算器(这可能非常简单,但找不到错误:( 叹气*),所以当我点击提交时,数字 1 和数字 2 将执行用户要求的操作已选中。但是提交时答案不会回显:( 什么问题?非常感谢!

<?php
if ($_POST['subBtn']) { 
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];

if($_POST['group1'] == 'add') {
echo $number1 + $number2 = $answer;
}
else if($_POST['group1'] == 'subtract') {
echo $number1 - $number2 = $answer;
}
else if($_POST['group1'] == 'times') {
echo $number1 * $num2ber = $answer;
} 
else if($_POST['group1'] == 'divide') {
echo $number2 / $number2 = $answer;

echo "<p>" . $answer . " </p>"; 
}
}
?>

<form name="number1" action="activity-2-4.php" method="post">
number1:<select name="number1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
        </select><br>

number2:<select name="number2">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
        </select><br>
<input type="radio" name="group1" id="add" value="add">+<br/>
<input type="radio" name="group1" id="subtract" value="subtract">-<br/>
<input type="radio" name="group1" id="times" value="times">x<br/>
<input type="radio" name="group1" id="divide" value="divide">/<br/>
<button type="submit" name="subBtn" id="answer" value="answer">Calculate</button>
</form>

更改分配值的方式。变化

  $number1 + $number2 = $answer;

  $answer = $number1 + $number2;

不要在 echo 语句中为变量赋值。此外,被赋值的变量应该总是在左边。另外,你的 echo 在 divide 的最后一个 elseif 里面。

试试这个:

<?php
if (isset($_POST['subBtn']) && isset($_POST['group1'])) { 
    $number1 = isset($_POST['number1']) ? $_POST['number1'] : 0;
    $number2 = isset($_POST['number2']) ? $_POST['number2'] : 0;

    switch ($_POST['group1']) {
        case 'add':
            $answer = $number1 + $number2;
            break;
        case 'subtract':
            $answer = $number1 - $number2;
            break;
        case 'times':
            $answer = $number1 * $number2;
            break;
        case 'divide':
            $answer = $number2 / $number2;
            break;
        default:
            $answer = 'No operation method selected.';
            break;
    }

    echo "<p>" . $answer . " </p>";
}
?>

我相信这是你的问题:

echo $number2 / $number2 = $answer

改为:

$answer = $number2 / $number2

每个数学运算都一样。

您试图在存储数据的同时显示某些内容,但顺序错误。

数学部分后你已经有了答案。

您需要检查 POST 变量是否存在。

你错误地使用了计算、变量和回显echo $number1 + $number2 = $answer;我猜你在这种格式下的预期输出4+1=5

<?php
if (isset($_POST['subBtn'])) 
{ 
    $number1 = isset($_POST['number1']) ? $_POST['number1'] : '';
    $number2 = isset($_POST['number2']) ? $_POST['number2'] : '';
    $group1  = isset($_POST['group1']) ? $_POST['group1'] : '';

    $answer  = '';
    $stmt    = '';

    switch ($group1)
    {
        case 'add': 
            $stmt   = $number1.'+'.$number2.'= ';
            $answer = $number1 + $number2;
        break;

        case 'subtract':    
            $stmt   = $number1.'-'.$number2.'= ';
            $answer = $number1 - $number2;
        break;

        case 'times':   
            $stmt   = $number1.'*'.$number2.'= ';
            $answer = $number1 * $number2;
        break;

        case 'divide':  
            $stmt   = $number1.'/'.$number2.'= ';
            $answer = $number1 / $number2;
        break;
    }

    echo "<p>" . $stmt.$answer . " </p>"; 
}
?>

<form name="number1" action="activity-2-4.php" method="post">
number1:<select name="number1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
        </select><br>

number2:<select name="number2">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
        </select><br>
<input type="radio" name="group1" id="add" value="add">+<br/>
<input type="radio" name="group1" id="subtract" value="subtract">-<br/>
<input type="radio" name="group1" id="times" value="times">x<br/>
<input type="radio" name="group1" id="divide" value="divide">/<br/>
<button type="submit" name="subBtn" id="answer" value="answer">Calculate</button>
</form>

$_POST 全局变量将所有内容存储为字符串。 理论上 PHP 确实在后端输入杂耍并将其转换为数字,但这对你来说不会发生,我不知道你是 PHP 的哪个版本 运行。所以你可以自己明确地转换它。 试试这个

$number1 = (float)$_POST['number1'];
$number2 = (float)$_POST['number2'];

如果你知道你得到的是一个整数,你可以做

$number1 = (int)$_POST['number1'];
$number2 = (int)$_POST['number2'];

不过我认为您的问题出在 group1 上。

print_r($_POST);

确保你有 group1 并确保它的小写值完全相同,因为你正在检查小写并且此操作区分大小写。

然后下一步是在屏幕上打印一些东西,并确保你在 if 中以

开始
echo "i'm in the add case";

等等