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?

2
  • You have to use eval(). That's usually an indicator that you should be doing something different. I suggest reviewing your strategy. Commented Mar 15, 2013 at 13:33
  • can you elaborate sir? and why you dont post in answer?hehe Commented Mar 15, 2013 at 13:34

4 Answers 4

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'; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 check numeric value. Nice & simple answer with more security.
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..)

1 Comment

For extra security you may check if $first and $second are integer and check if $operator is a valid operator if(in_array($operator, array('+', '-', '*', '/', '%'))) then execute the eval function
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.

Comments

-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; ?> 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.