-2

For example somebody write on site into input 2+2 and then variable goes to php by GET. Php print variable as 2+2. How to automatically convert it to result of given calculation (4)?

Jakub K.
  • 3
  • 3
  • You need to write code that parses it and performs the appropriate calculation. You could use `eval()`, but that opens up an enormous can of worms. – Barmar Jun 09 '17 at 19:56
  • See https://stackoverflow.com/questions/951373/when-is-eval-evil-in-php?s=1|0.0000 for why `eval()` is not a good idea. – Barmar Jun 09 '17 at 19:57

2 Answers2

3

Unless you want to rely on eval (I don't recommend it), or want to include/design code for recognizing arbitrary arithmetic operators or any combination of them (with or without but not limited to using parenthesis), I would recommend using a math parser such as https://github.com/mossadal/math-parser. You can install it using composer: https://getcomposer.org/doc/00-intro.md.

Ruben Vincenten
  • 807
  • 4
  • 7
-2

Try to check the input field for existing + sign in the input value posted.

If yes then you can simply add the two numbers

Gary Singh
  • 57
  • 3
  • Ok, thanks i didnt thought about this but what if there is more? Like 2*2+2. – Jakub K. Jun 09 '17 at 20:08
  • Not really helpfull as this doesn't describe how to add both numbers in code, and I think OP has already figured out that numbers need to be added if one is using the plus symbol. – Ruben Vincenten Jun 09 '17 at 20:16