I have a problem with my calculator

-1

I have the following problem: The $ re variable does not work for me as I seek to do so. I try to get the variable $ re to obtain the values corresponding to the operation that the user establishes, either by addition, subtraction ... etc.

The errors I missed were that the variable is not defined or has no value, I do not know how to make php recognize the variable and its corresponding content.

Php code:

<?php
if (isset($_POST["n1"]) && isset($_POST["n2"])) {
    if ($_POST["op"] == "+") {
        return $re = $_POST["n1"] + $_POST["n2"];
    }else
    if ($_POST["op"] == "-") {
        return $re = $_POST["n1"] - $_POST["n2"];
    }else
    if ($_POST["op"] == "*") {
        return $re = $_POST["n1"] * $_POST["n2"];
    }else
    if ($_POST["op"] == "/") {
        return $re = $_POST["n1"] / $_POST["n2"];
    }
}


require "views/index.view.php";

and the html code I can not copy it here because instead of showing it, it executes it.  

    
asked by Alex Fath 03.12.2018 в 03:26
source

1 answer

1

Look at this and execute it in your local '     

Calculator

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
    <div>
        <input type="text" name="n1" value="">
        <select name="op" size="1">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" name="n2" value="">
        <input type="hidden" name="re">
    </div>
        <input type="submit" name="submit">
        <?php 
            if (isset($_POST["submit"])) {
                if (isset($_POST["n1"]) && isset($_POST["n2"])) {
                    echo "<br>Resultado: ";
                    if (isset($_POST["op"])=="+") {
                        echo $_POST["n1"]+$_POST["n2"];
                    }
                    if (isset($_POST["op"])=="-") {
                        echo $_POST["n1"]-$_POST["n2"];
                    }
                    if (isset($_POST["op"])=="*") {
                        echo $_POST["n1"]*$_POST["n2"];
                    }
                    if (isset($_POST["op"])=="/") {
                        echo $_POST["n1"]/$_POST["n2"];
                    }
                }
            }

         ?>

</form>

'

    
answered by 03.12.2018 в 03:45