1

I have a string variable, $operation, that can have values like + or - and two integer variables $initial and $unit.

So to echo the result of the arithmetic operation between them

I have to use something like

 if($operation == '+') echo ($initial + $unit);
 if($operation == '-') echo ($initial - $unit);

Is there a way I can do this without the IF?

4
  • 1
    possible duplicate of Variable Operators in PHP Commented Jan 17, 2011 at 23:10
  • 1
    from the various different answers so far, i'd ask why?. Commented Jan 17, 2011 at 23:14
  • yes, it was pretty stupid to ask this:) I'll stick with the shorter IF example that you posted. Commented Jan 17, 2011 at 23:24
  • If you only have to choose between + and - you should use the ternary conditional operator as some people suggested (?:). Commented Jan 17, 2011 at 23:36

4 Answers 4

3

trickery with math:

echo $initial + (($operation == '-') ? -1 : 1) * $unit;

only using addition, but cheating with multiplying by a negative... :)

Sign up to request clarification or add additional context in comments.

Comments

3

You could use a map, i.e.

function add($a, $b) { return $a + $b; }
function sub($a, $b) { return $a - $b; }

$operations = array('+' => 'add', '-' => 'sub');

$operations[$operation]($initial, $unit);

Comments

1
echo ($operation == '+') ? $initial + $unit : $initial - $unit;

Comments

0

With eval.

But make sure you do your whitelist validations before feeding anything to eval.

if(in_array($operation, array('+', '-'))){
    eval('echo $initial '.$operation.' $unit;');
}

11 Comments

While you can use eval(), you really shouldn't. Especially when there are better options available.
"But make sure you do your whitelist validations before..." Does this tell anything to you? I added an example. Please enlighten me and explain what is wrong with it. "you really shouldn't" a lot of things in PHP if you don't do your validations. Following that principle you shouldn't put user supplied input in databases, but some people still tend to do so.
Even with a whitelist (and assuming you can even write a whitelist that can handle possible inputs), eval() runs considerably slower on PHP than simply mapping the operation "name" to a function.
I didn't claim it's the best answer. But it is worth considering if you know your way with "teh codez".
@Alin Did you even read my first few comments? My argument is that it's bad because it's slower. Considerably slower. @netcoder Uhh. Did you even time it? I come out to an order of magnitude of difference with only 100000 iterations. My test script: pastebin.com/s4ZDS48M While I agree that SO is not the place to start an argument, this is entirely relevant to the question at hand.
|

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.