Faouzi Nikolaic Faouzi Nikolaic - 1 year ago 112
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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download