3

So I have a simple site that is using php to do some simple math like so:

<form action="" method="POST">
<input type="text" name="first" />
<select name="method">
    <option>+</option>
    <option>_</option>
    <option>*</option>
    <option>/</option>
</select>
<input type="text" name="second" />
<input type="submit" value="Equals" />

The site lets the user inputs two numbers and then select a math operator to manipulate them by. So for example in the first input, the user could enter the number 3, select the subtraction operator, enter the number 1 in the next field and submit. I process the information like so:

if (isset($_POST['first']) && isset($_POST['second'])) {
   $first = $_POST['first'];
   $second = $_POST['second'];
   $method = $_POST['method'];
} 

However, I want to echo the result of the math problem on webpage after submitting. How would I do this? Just echoing all the variables would just give me (in this case) "3-1" instead of the actual answer which would be "2".

Any idea how would accomplish this?

2

4 Answers 4

6
if (isset($_POST['first']) && isset($_POST['second'])) {
    $first = $_POST['first'];
    $second = $_POST['second'];
    $method = $_POST['method'];
    switch($method)
    {
        case '+':
            $result = $first + $second;
            break;
        case '-':
            $result = $first - $second;
            break;
        case '*':
            $result = $first * $second;
            break;
        case '/':
            // check for division by 0 (wouldn't want to blow anything up)
            $result = $first / $second;
            break;
        default:
            $result = 'undefined operation';
            break;
    }
    printf("%.5f %s %.5f = %s", $first, $method, $second, $result);
}
Sign up to request clarification or add additional context in comments.

3 Comments

$second is an integer, so you probably want to use %d instead of the second %s.
@Arjan: how do you know it's an integer? Nowhere does he say the values are integers. I would probably add checks is_numeric to both arguments.
The numeric inputs are cast to integers in lines 2 and 3 of Tims code.
2

You need to use a switch statement on your $method like so:

switch($method){

    case '+':
        $result = $first + $second;
        break;

    case '-':
        $result = $first - $second;
        break;

    case '*':
        $result = $first * $second;
        break;

    case '/':
        $result = $first / $second;
        break;

}
var_dump($result);

Alternatively, you could also EVAL the operation:

$code = '$result = '.$first.$method.$second.';';
exec($code);
var_dump($result);

Note that you don't check for your input, it could lead to security issues:

if(!isset($_POST['first']) || !is_numeric($_POST['first'])){ exit('Bad number in #1'); }

Do the same for other input such as first, second and method :)

Comments

2

you could use a switch case.

switch($method){

case "+":
    $result = $first + $second;
    break;
case "-":
    $result = $first - $second;
    break;
case "*":
    $result = $first * $second;
    break;
case "/":
    $result = $first/$second;
    break;
}

echo $result;

2 Comments

Please format your answer? This really hard to read for most people that will look at your answer
I hit submit early I finished it
1

It's dangerous to just evaluate the expression, as it could contain nasties and you should never trust user input (even though you're using a select tag someone could inject anything).

Either sanitize the input first then evaluate it (e.g. check the two numbers are actually numbers, and the method is only one of an acceptable list), or use a switch statement on the method variable and write out each method in PHP.

Alternatively you can look at scripting languages inside PHP such as LUA.

Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.