Process mathematical equations in php

后端 未结 8 1905
陌清茗
陌清茗 2020-11-30 14:14

A user is allowed to enter any mathematical equation they like (with one variable):

x + 5

1 - x/2

(x/3) * (56/13)

8条回答
  •  無奈伤痛
    2020-11-30 14:40

    eval()

    Depends what you have to do but anyway, the cheapest way to doing it's using a replace function for the variables and then run the expression by using the eval().
    Of course you need first to make sure that your formulas are in php syntax.
    The good thing is that you can use any mathematical function supported by php, the bad thing is, it's never nice to use the eval() :)

    PHPClasses

    The other good option it's surfing the web until you find a parser :P
    http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html

提交回复
热议问题