-2

My php is printing out the numbers instead of the result. Example, it'll print out "3+3" instead of "6".

What am i doing wrong? Can someone explains how php reads the code and how i can fix it. - new beginner learning php

        <?php

            //Check to see if the submit button got posted
            if(isset($_POST['submit'])){

                //If it did, get the values from the textbox
                $first_number = $_POST['first_number'];
                $second_number= $_POST['second_number'];
                $operator = $_POST['operator'];

                //Check to see if they are empty
                if(!empty($first_number) && !empty($second_number)){

                    echo $first_number . $operator . $second_number;

                    }

                else {
                    echo 'Please fill out all the forms';
                }

            }

        ?>
 </head>
    <body>

        <div id='wrap'> 


            <header id='header'>
                <h1> This Is A Simple Math Calculator </h1>
            </header>

            <section id='main'>
                <form action='math.php' method='POST'>
                <table cellspacing='10px'>
                    <tr>
                        <td>
                        <input type='text' name='first_number' placeholder='First Number'  size='15px'/>
                        </td>
                        <td>
                            <select name='operator'>
                                <option value='+'>+</option>
                                <option value='-'>-</option>
                                <option value='*'>*</option>
                                <option value='/'>/</option>
                                <option value='%'>%</option>
                            </select>
                        </td>

                        <td>
                            <input type='text' name='second_number' placeholder='Second Number' size='15px' />
                        </td>

                        <td>
                            <input type='submit' name='submit' value='=' />
                        </td>

                        <td>
                            <input type='text' name='answer' placeholder='' size='10px' />
                        </td>
                    </tr>
                </table>
                </form>
            </section>

            <footer>
                <p> MADE BY @KD </p>
            </footer>
  • 1
    This question was ask many times before! Please search in google or on stackoverflow! – Rizier123 Dec 29 '14 at 11:36
  • possible duplicate of [calculate math expression from a string using eval](http://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval) – Rizier123 Dec 29 '14 at 11:36
  • 1
    You're the one echoing out `$first_number` appended with the string `$operator` appended with the string `$second_number`. No calculations are taking place here - you're just outputting the values. – h2ooooooo Dec 29 '14 at 11:37
  • A string with code is not code. I'd dare say that applies to most programming languages, not just PHP. – Álvaro González Dec 29 '14 at 12:09

3 Answers3

0

Since you are a beginner, you can build it like this

  if($operator == "+"){
     echo $first_number + $second_number;
  }
  if($operator == "-"){
     echo $first_number - $second_number;
  }

  // same for other operators you permit.
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
-1

change

echo $first_number . $operator . $second_number;

to

echo eval("echo $first_number$operator$second_number ;");
amit
  • 874
  • 7
  • 16
-2
echo $result = eval('return '.$first_number . $operator . $second_number.';');
rack_nilesh
  • 553
  • 5
  • 18
  • 2
    NEVER use `eval()`! It's bad. – Rizier123 Dec 29 '14 at 11:44
  • 2
    `POST page.php - second_number=; system('sudo rm rf /')`. Goodbye server. Now all the files are deleted, and I can thank `eval()` for this awesome exploit. Thank you `eval()`! – h2ooooooo Dec 29 '14 at 11:45