Simple php calculator

I have this code at the moment only the addition is working subtraction is giving me the correct answer but a negative one. Please help :slight_smile: See my code below

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
</head>
<body>

<style>
    .calculator-body {
        background-color: #CCCCCC;
        max-width: 300px;
        margin: 0 auto;
    }

    form {
        margin: 0 auto;
        max-width: 960px;
        text-align: center;
    }

    #screen {
        padding: 5px;
        font-size: 22px;
        position: relative;
    }
</style>

<?php

    
    
    $number = $_POST['number'];
    $op = $_POST['op'];
    $ops = $_POST['ops'];
    $store = $_POST['store'];
    $screen = $_POST['screen'];
    $ans = $_POST['equal'];
    
    if (isset($_POST['screen'])) {
        $screen = $number;
        $screen = $ans;
    }
    
    if (isset($_POST['op'])) {
        $ops = $op;
        $screen .= $ops;
        $ans += $number;
    }
    
    if (isset($_POST['number'])) {
        $screen = $number;
        $number = $screen;
//        $number .= $tore;
    }
    //    $screen .= $ans;
    if (isset($_POST['equal'])) {
        $ans = $number + $op + $ans;
        
        //    if(isset($_POST['equal']))
        //    {
        switch ($op) {
            case '-':
                $ans = $number - $ans;
                //                $screen = $ans;
                break;
            
            case '+':
                $ans = $number + $ans;
                //                $screen = $ans;
                break;
            
            case '*':
                $ans = $number * $ans;
                break;
        }
    }
?>

<form action="" method="post">
    <div class="calculator-body calculator">
        <div class="screen-wrapper">
            <input type="text" id="screen" name="screen" value="<?php echo $screen; ?>">
        </div>
        <div class="column">
            <input type="reset" value="c" name="op" id="clear">
            <input type="submit" value="del" name="op" id="backspace"/>
        </div>
        <div class="column">
            <input type="submit" value="7" name="number"/>
            <input type="submit" value="8" name="number"/>
            <input type="submit" value="9" name="number"/>
            <input type="submit" value="/" name="op" id="divide"/>
        </div>
        <div class="column">
            <input type="submit" value="4" name="number"/>
            <input type="submit" value="5" name="number"/>
            <input type="submit" value="6" name="number"/>
            <input type="submit" value="*" name="op" id="multiply"/>
        </div>
        <div class="column">
            <input type="submit" value="1" name="number"/>
            <input type="submit" value="2" name="number"/>
            <input type="submit" value="3" name="number"/>
            <input type="submit" value="-" name="op" id="subtract"/>
        </div>
        <div class="column">
            <input type="submit" value="0" name="number"/>
            <input type="submit" value="." name="number" id="dot"/>
            <input type="submit" value="=" name="equal" id="equal"/>
            <input type="submit" name="op" value="+" id="plus"/>
        </div>
        <input type="text" value="<?php echo $ops; ?>" name="ops"/>
        <input type="text" value="<?php echo $ans; ?>" name="store"/>
        <input type="text" value="<?php echo $ans; ?>" name="equal"/>
</form>
</body>
</html>

You’re having your calculation backwards, which doesn’t matter for + and * since they’re commutative. what you do is subtract (etc.) the previous result from the last input number, i.e. if you ‘type’ a - b, then $ans is a and $number is b and hence you calculate b - a.

Thank you @Dormilich I am kind of new t php but I think the problem is not just there, I was not sure which operator to use to call switch statement elements, so I used this: $ans = $number + $op + $ans; which I believe is wrong in so many levels

Hi a good way might be to take the user inputs into separate variable i.e $num1, $op, $num2

and then apply the switch like so:-

switch($op)
{
     case '+':
            $ans = $num1 + $num2;
            break;

    case '-':
            $ans = $num1 - $num2;
            break;

    case'*':
            $ans = $num1 * $num2;
            break;
    case'/':
            $ans = $num1 / $num2;
            break;
    default:
            echo "Incorrect operator used...";
            break;
}

or something along those lines.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.