Re: Mathematical expression calculation function?
| From: | John W. Holmes | Date: | Thu, 22 Apr 2004 15:37:49 +0000 |
| Subject: | Re: Mathematical expression calculation function? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184254@lists.php.net to get a copy of this message | ||
From: "Enfors Christer" <chenf@wmdata.com>
> Hi. I need a function that takes a mathematical expression
> in the form of a string, and calculates the result. PHP itself
> doesn't seem to provide one. I need something which can
> handle strings like:
>
> 10*73.2+3-(4*358.2874)/352
$str = "10*73.2+3-(4*358.2874)/352";
eval('$result = ' . $str . ';');
echo $result;
> ... and so on. If it handles functions (like cos(), pow(),
> exp()) too, then that would be great.
That will. Depending on what $str is coming from, make sure you're
validating it somewhat so there's not any malicious PHP code injected (for
example $str = 'file_get_contents("secretfile.txt")' )
---John Holmes...