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?

codedude
  • 6,244
  • 14
  • 63
  • 99
  • 2
    possible duplicate of [how to evaluate formula passed as string in php?](http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php) – Ben Lee Nov 30 '11 at 20:25
  • I'm not sure I really understood how to apply that solution to my answer. – codedude Nov 30 '11 at 20:26

4 Answers4

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);
}
  • `$second` is an integer, so you probably want to use `%d` instead of the second `%s`. – Arjan Nov 30 '11 at 20:31
  • @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. – Aleks G Nov 30 '11 at 20:35
  • The numeric inputs are cast to integers in lines 2 and 3 of Tims code. – Arjan Nov 30 '11 at 20:42
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;
Laurence Burke
  • 2,348
  • 14
  • 26
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 :)

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
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

Tak
  • 11,428
  • 5
  • 29
  • 48