Simple PHP calculator

前端 未结 11 784
独厮守ぢ
独厮守ぢ 2020-12-14 12:01

I\'m creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it\'s not outputting

相关标签:
11条回答
  • 2020-12-14 12:44
    <?php
    $cal1= $_GET['cal1'];
    $cal2= $_GET['cal2'];
    $symbol =$_GET['symbol'];
    
    
    if($symbol == '+')
    {
        $add = $cal1 + $cal2;
        echo "Addition is:".$add;
    }
    
    else if($symbol == '-')
    {
        $subs = $cal1 - $cal2;
        echo "Substraction is:".$subs;
    }
    
     else if($symbol == '*')
    {
        $mul = $cal1 * $cal2;
        echo "Multiply is:".$mul;
    }
    
    else if($symbol == '/')
    {
        $div = $cal1 / $cal2;
        echo "Division is:".$div;
    }
    
      else
    {
    
        echo "Oops ,something wrong in your code son";
    }
    
    
    ?>
    
    0 讨论(0)
  • 2020-12-14 12:45

    Personally I would do a switch instead of all this if, else if, else

    $first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
    $second= $_POST['second'] + 0;
    $operator = $_POST["group1"];
    switch($operator)
    {
        case "add"
        echo "Answer is: " .$first + $second;
        break; 
        case "subtract"
        echo "Answer is: " .$first - $second;
        break;
        case "times"
        echo "Answer is: " .$first * $second;
        break; 
        case "divide"
        echo "Answer is: " .$first / $second;
        break;
    }
    
    0 讨论(0)
  • 2020-12-14 12:47
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
        <head>
            <meta charset="utf-8">
            <title>Calculator</title> 
        </head>
    <body>
    
        <form method="GET">
        <h1>Calculator</h1>
        <p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
            <ul>
         <ol> Multiplication: * </ol>
         <ol> Substraction: - </ol>
         <ol> Division: / </ol>
         <ol> Addition: + </ol>
            </ul>
         <p>Enter first number:</p>
            <input type="text" name="cal1">
            <p>Enter second number:</p>
            <input type="text" name="cal2">
            <p>Enter the calculator symbol</p>
            <input type="text" name="symbol">
            <button>Calculate</button>
        </form>
        <?php
        $cal1= $_GET['cal1'];
        $cal2= $_GET['cal2'];
        $symbol =$_GET['symbol'];
    
        if($symbol == '+')
        {
            $add = $cal1 + $cal2;
            echo "Addition is:".$add;
        }
        else if($symbol == '-')
        {
            $subs = $cal1 - $cal2;
            echo "Substraction is:".$subs;
        }
         else if($symbol == '*')
        {
            $mul = $cal1 * $cal2;
            echo "Multiply is:".$mul;
        }
        else if($symbol == '/')
        {
            $div = $cal1 / $cal2;
            echo "Division is:".$div;
        }
          else
        { 
            echo "Oops ,something wrong in your code son";
        }
        ?>
        </body> 
    </html>
    
    0 讨论(0)
  • 2020-12-14 12:48
    $first = doubleval($_POST['first']);
    $second = doubleval($_POST['second']);
    
    if($_POST['group1'] == 'add') {
        echo "$first + $second = ".($first + $second);
    }
    
    // etc
    
    0 讨论(0)
  • 2020-12-14 12:51

    Make this changes in php file

    <html>
    <head>
    <meta charset="utf-8">
    <title>Answer</title>
    </head>
    <body>
    <p>The answer is: 
    <?php
    $first = $_POST['first'];
    $second= $_POST['second'];
    if($_POST['group1'] == 'add') {
    echo $first + $second;
    }
    else if($_POST['group1'] == 'subtract') {
    echo $first - $second;
    }
    else if($_POST['group1'] == 'times') {
    echo $first * $second;
    }
    
    else if($_POST['group1'] == 'divide') {
    echo $first / $second;
    }
    else {
        echo "Enter the numbers properly";
    }
    ?>
    </p> 
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题