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?

Alex
  • 66,732
  • 177
  • 439
  • 641

4 Answers4

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);
Jeff Hubbard
  • 9,822
  • 3
  • 30
  • 28
3

trickery with math:

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

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

John Gardner
  • 24,225
  • 5
  • 58
  • 76
1
echo ($operation == '+') ? $initial + $unit : $initial - $unit;
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
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;');
}
Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
  • While you can use eval(), you really shouldn't. Especially when there are better options available. – Jeff Hubbard Jan 17 '11 at 23:10
  • 1
    "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. – Alin Purcaru Jan 17 '11 at 23:11
  • i think your edit with example makes it more obvious + safer. But you always have the danger that someone sees the eval link and never comes back to get the whitelist part :) – John Gardner Jan 17 '11 at 23:19
  • 2
    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. – Jeff Hubbard Jan 17 '11 at 23:20
  • 1
    I didn't claim it's the best answer. But it is worth considering if you know your way with "teh codez". – Alin Purcaru Jan 17 '11 at 23:24
  • By your logic, it's perfectly fine to go willy nilly and use whatever you want to anywhere, any time. Just so long as "you know your way with teh codez". Ideas like that are what lead to buffer overflows, XSS, CSRF, etc. – Jeff Hubbard Jan 17 '11 at 23:27
  • @Jeff If you want to quote me do it properly: "you know your way with 'teh codez.'" As for the rest of the comment: I don't see any arguments. – Alin Purcaru Jan 17 '11 at 23:32
  • 1
    @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: http://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. – Jeff Hubbard Jan 17 '11 at 23:36
  • @Jeff Because it's slower it leads to buffer overflows, XSS, CSRF, etc.? You have some logic holes there. Over and out ;) – Alin Purcaru Jan 17 '11 at 23:47
  • ...*sigh* Forget I said anything. – Jeff Hubbard Jan 17 '11 at 23:49
  • no, keep going! it was *just* starting to get interesting! you didn't even get to the name calling part yet! :D – John Gardner Jan 18 '11 at 00:19