3

guys this is a simple question for most of you probably. but im confused on how can i perform operations with operator symbol saved in a variable. Example.

$first=5;
$second=5;
$operator="+";

$result=$first.$operator.$second;

echo $result;

but $result will just print 5+5. i want it to perform the operation.

my idea is to put it all operations in an if condition -> if($operator == '+'){add the first and second operand}. any other ideas guys?

Belmark Caday
  • 1,623
  • 3
  • 21
  • 29

4 Answers4

8

Instead of using eval(), you may try a custom function with a switch() inside:

$first = 5;
$second = 3;
$operator = '+';

$result = mathOp($operator, $first, $second);
echo $result;

function mathOp($operator, $n1, $n2){
    if(!is_numeric($n1) || !is_numeric($n2)){
        return 'Error: You must use numbers';
    }
    switch($operator){
        case '+':
            return($n1 + $n2);
        case '-':
            return($n1 - $n2);
        case '*':
            return($n1 * $n2);
        case '/':
            if($n2 == 0){
                return 'Error: Division by zero';
            }else{
                return($n1 / $n2);
            }
        default:
            return 'Unknown Operator detected';
    }
}
HamZa
  • 14,671
  • 11
  • 54
  • 75
1

You have to use eval() which executes a PHP operation.

$first=5;
$second=5;
$operator="+";

$term = $first.$operator.$second;
eval("$result = " . $term);

echo $result;

But be careful with eval it executes every PHP function. (Even exec..)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
idmean
  • 14,540
  • 9
  • 54
  • 83
  • For extra security you may check if $first and $second [are integer](http://www.php.net/manual/en/function.is-int.php) and check if $operator is a valid operator `if(in_array($operator, array('+', '-', '*', '/', '%')))` then execute the eval function – HamZa Mar 18 '13 at 10:15
0

Eval is one solution, but there are others. If your search SO for math parser, you will find many answers. This one is a good example.

So basically, your choices are: eval() with some validation first, or a custom math parser that does it for you, such as evalMath.

If your are looking only for simple operations (+, -, *, /) and two operators, use a validation function for your parameters (numbers only, known operators only) and use eval. If your want to be able to parse complex operations (parenthesis, variables...) then go with a parser.

Community
  • 1
  • 1
jaudette
  • 2,305
  • 1
  • 20
  • 20
-8

You are not far off with your own syntax, when you use

$first.$operator.$second;

You are literally adding the strings together, you need to use the '+' operator to successfully use your method. See below code, tried and tested

    $first = 5;
    $second = 5;
    $operator = '+';

    $result = $first+$operator+$second;

    echo $result;

?>
Martin
  • 92
  • 8
  • 6
    What you're doing here is just adding two variables like usual. Let me explain: `$result = $first+$operator+$second;` PHP checks the value of $first which is 5, then PHP checks the value of $operator, but oops it's a string, so PHP converts it to a number which is 0 `echo intval('+');`, and then PHP checks $second which is 5 so the equation becomes 5+0+5 = 10. So for example if you do `$operator = '*';` It will always return 10. – HamZa Mar 16 '13 at 22:22