3 months ago by Nafeel Rajput

I made a calculator but only an addition operation is working.

Here is code

        <form>
        <input type = "text" name = "num1"/>
        <input type = "text" name = "num2"/>
        <select name = "opr">
        <option>Add</option>
        <option>MULTIPLY</option>
        <option>SUBTRACT</option>
        <option>DIVIDE</option>
        </select>
        <br>
        <button type ="submit" name ="submit" value = "submit">submit</button>
        </form>
        <?php
        if(isset($_GET['submit']))
        {
        $num1 = $_GET["num1"];
        $num2 = $_GET["num2"];
        $opr =  $_GET["opr"];

        if($opr = "ADD")
        {
            $num3 = $num1+$num2;
            echo $num3;
        }
        elseif($opr = "MULTIPLY" )
        {
            $num3 = $num1*$num2;
            echo $num3;
        }
        elseif($opr = "SUBTRACT" )
        {
            $num3 = $num1-$num2;
            echo $num3;
        }
        else{
            $num3 = $num1/$num2;
            echo $num3;
        }
        }

        ?>

I also made a conditional statement for other operations too but now sure why it's not working. I am trying to get values from URL using a $_GET function. But only ADD method is working.

3 months ago by Juan Cadima

Best answer

You have your if statements wrong:

if ( $opr = "ADD" )
is wrong, you are assigning "ADD to $opr variable

should be:

if ( $opr == "ADD" )

change to all of your if statements

Sign in or create an account to reply to this topic.