Code Monkey home page Code Monkey logo

php-formula-interpreter's Introduction

php-formula-interpreter

A formula interpreter for php

Why this library ?

Some user could wants to perform a simple calculation and being able to change it as much as he can. Before using a library, you could use the eval function. But this method has two major drawbacks :

  • Security. A php script is being evaluated by the eval function. Php is a very powerful language, perhaps too powerful for a user especially when the user wants to inject malicious code.

  • Complexity. Php is also complex for someone who doesn't understand programming language. It could be nice to interpret an excel-like formula instead.

How does it work ?

First, create an instance of FormulaInterpreter\Compiler

$compiler = new FormulaInterpreter\Compiler();

Then use the compile()method to parse the formula you want to interpret. It will return an instance of FormulaInterpreter\Executable :

$executable = $compiler->compile('2 + 2');

Finally run the formula from the executable :

$result = $executable->run();
// $result equals 4

Using operators

Operator multiplication (*) and division () are being evaluted first, then addition (+) and subtraction (-)

You can also force the prioriry of an expression by using parentheses like this

'2 * (3 + 2)'

You can use as many parentheses as you like.

'2 * (2 * (3 + 2 * (3 + 2)) + 2)'

Others operators like modulo, power, etc. will be implemented in the future as functions.

Using variables

A variable is just a word inside your formula like this :

'price * rate / 100'

Just before executing a formula, make sure to inject all the required variables in an array

$variables = array(
   'price' => 40.2,
   'rate' => 12.8
);

$executable->run($variables);

Using functions

Here is an example of expression using a function :

   'cos(0)'

Available functions : pi, pow, cos, sin, sqrt & modulo

You can embed functions as much as you like

   'pow(sqrt(4), 2)'

php-formula-interpreter's People

Contributors

sander-toonen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.