0

I have a string formula which I fetch from a file :

  $text = "100*20";

I want to find a way to convert this formula into its result, for instance have the value 200. Is this possible using php?

Faouzi FJTech
  • 981
  • 3
  • 13
  • 27

2 Answers2

-1

Use eval().

$expression = '100*20';
eval('$result = ('.$expression.')');
echo $result;
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • No no no. Don't direct people to use `eval` for math operations especially when there are so many solutions out there that are actually safe to use. – Jon May 23 '14 at 22:10
-1

Make sure to investigate the security issues surrounding eval();.

As the data is from a file it may be ok, but if it is user input - don't.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. If you [earn](//meta.stackoverflow.com/q/146472/169503) sufficient [reputation](//stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](//stackoverflow.com/help/privileges/comment). – Blue Aug 05 '16 at 14:06