Faouzi Nikolaic Faouzi Nikolaic - 1 year ago 77
PHP Question

Convert a string formula into its result php

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?

Answer Source

Use eval().

$expression = '100*20';
eval('$result = ('.$expression.')');
echo $result;