0

我创建了一个 php 计算器,但是当您尝试将两个数字相除时,它会将它们相乘

链接到代码= http://pastebin.com/raw.php?i=9QtmJmQY

<?php

?>

<html>
<body>

<?php
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = '';
}

class calc{
    var $number1;
    var $number2;


        function add($number1,$number2)
        {
            $result = $number1 + $number2;
            echo("The sum of $number1 and $number2 is $result<br><br>");
            echo("$number1 + $number2 = $result");
            exit;
            }

        function subtract($number1,$number2)
        {
            $result = $number1 - $number2;
            echo("$number1 subtract $number2 is $result<br><br>");
            echo("$number1 - $number2 = $result");
            exit;
            }

        function multiply($number1,$number2)
        {
            $result  = $number1 * $number2;
            echo("$number1 multiplied by $number2 is $result<br><br>");
            echo("$number1 * $number2 = $result");
            exit;
            }

        function divide($number1,$number2)
        {
            $result = $number1 / $number2;
            echo("$number1 divided by $number2 is $result<br><br>");
            echo("$number1 / $number2 = $result");
            exit;
        }
}
$calc = new calc();
            ?>

<title>php object oriented calculator</title>
<form name="calc" action="?page=calc" method="POST">
Number 1: <input type=text name=value1><br>
Number 2: <input type=text name=value2><br>
Operation:

<input type=radio name=oper value="add">Addition 
<input type=radio name=oper value="subtract">Subtraction 
<input type=radio name=oper value="divide">Division
<input type=radio name=oper value="multiply">Multiplication<br>
<input type=submit value="Calculate">
</form> 

<?php
if($page == "calc"){
$number1 = (int)$_POST['value1'];
$number2 = (int)$_POST['value2'];
$oper = $_POST['oper'];
    if(!$number1){
        echo("You must enter number 1!");
        exit;
        }
        if(!$number2){
            echo("You must enter number 2!");
            exit;
            }
        if(!$oper){
            echo("You must select an operation to do with the numbers!");
            exit;
            }

        if($oper == "add"){
            $calc->add($number1,$number2);
            }

        if ($oper =="subtract"){
            $calc->subtract($number1,$number2);
            }
        if ($oper = "multiply"){
            $calc->multiply($number1,$number2);
            }
        if ($oper = "divide"){
            $calc->divide($number1,$number2);
            }
            }
            ?>
</body>
</html>
4

1 回答 1

3

您正在使用分配而不是比较

您的代码(错误!):

if ($oper = "multiply")

正确的代码(你真正想做的):

if ($oper == "multiply")

划分也一样。

于 2013-07-23T18:13:55.653 回答