0

I have a project where I calculate statistics of various fields and display them in tables. Now since this project is meant for 1 company I want to make it generic so that it can be used by others as well.

For example, I want to have a function that takes pattern as parameter and array of values and then calculates based on that pattern, for example:

  $pattern = 'a-b*c';
  $a = 5;
  $b = 1;
  $c = 2;
  $values  = array($a, $b, $c);

  calculateGivenPattern($pattern, $values); 

and above would give result:

 5 - 1 * 2 = 3

Is such thing possible in PHP? I am not asking for code, just need a name of algorithm or design pattern that will help me design this.

Thanks alot

GGio
  • 7,441
  • 11
  • 41
  • 76
  • 1
    You're looking for a *parser*. Try "math parser", "math operator parsing", "formula parser", something like that. – Geobits Oct 02 '13 at 17:17
  • or infix/postfix algorithms might work. I've written them in Java wont be hard to make it in PHP. Do you think they might work? – GGio Oct 02 '13 at 17:18
  • You should look at [this question](http://stackoverflow.com/questions/17774360/php-variables-infix-to-prefix-to-postfix) – Jacob S Oct 02 '13 at 17:27
  • You could do something like it using `eval`. – Barmar Oct 02 '13 at 17:29

0 Answers0