[PHP] Mathematical expression calculation function?

2004-04-22 Thread Enfors Christer
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

... and so on. If it handles functions (like cos(), pow(), exp()) too, then that would 
be great.

I'd appreciate any help. Thanks in advance.


Re: [PHP] Mathematical expression calculation function?

2004-04-22 Thread John W. Holmes
From: Enfors Christer [EMAIL PROTECTED]

 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...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php