Math evaluation for strings.
TinyExpr Bindings for PHP
TinyExpr is a very small parser and evaluation library for evaluating math expressions from C
git clone --branch "master" --depth 1 https://github.com/MehrAlsNix/ext-tinyexpr.git /tmp/tinyexpression
cd /tmp/tinyexpression
phpize
./configure
make
make install
echo "extension=tinyexpression.so" > /usr/local/etc/php/conf.d/tinyexpression.ini
tbd.
(TinyExpression 0.1.0)
tinyexpr_interp - Evaluate math expressions from a string
.
tinyexpr_interp ( string $expr ) : float
expr string
Returns the evaluated math expression as a float
.
docker-compose build
docker-compose up -d
docker-compose exec php bash
php -r "var_dump(tinyexpr_interp('2*9+8-(5/9)'));"
float(25.444444444444)
php -r "var_dump(tinyexpr_interp('sin(0.6)'));"
float(0.56464247339504)
Not parsable expressions will return NAN
:
php -r "var_dump(tinyexpr_interp('(1+2'), is_nan(tinyexpr_interp('(1+2')));"
float(NAN)
bool(true)