-2

Possible Duplicate:
PHP: Calculate a math function f(x) in a string

please anyone help me to solve a math function, for example i want to solve 3x+2y+4z the value of the variable should be declared in php, like $x=4, $y=2, $z=1 i know that there is PHP: Calculate a math function f(x) in a string , but is there any simple method ?

Community
  • 1
  • 1
Mafaik
  • 221
  • 1
  • 2
  • 5
  • The simple method is to use `eval`. The harder method is to build a parser. See the accepted answer for http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence – Salman A Nov 06 '12 at 15:37
  • Already answered in [PHP: Calculate a math function f(x) in a string](http://stackoverflow.com/questions/4216858/php-calculate-a-math-function-fx-in-a-string) – deceze Nov 06 '12 at 15:37
  • Why does the other answer not satisfy your question? This is a somewhat complex problem, how much simpler does it need to be? – deceze Nov 06 '12 at 15:38
  • exactly; what do you mean with "solve"? Actually the string "3x+2y+4z" you posted is not an equation.
    However, you need a parser (Chomsky type-2 language), create the tree representation of you formula and then proceed with the computations. It's pretty much like a simple compiler...
    – Gianluca Ghettini Nov 06 '12 at 15:39
  • Why is it not an equation? Also, this is not answer but a comment. – deceze Nov 06 '12 at 15:45
  • equations are of the form f1(x1,...,xN) = f2(y1,...,yN). basically they must provide the sign '=' – Gianluca Ghettini Nov 06 '12 at 15:50
  • Ah, of course, you're right. A "statement" it is. :) – deceze Nov 06 '12 at 15:52

1 Answers1

0

You can just create a simple equation:

$val = (3*$x)+(2*$y)+(4*$z)
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62