✖️ `math_eval` safely evaluates mathematical expressions
Clone or download
Latest commit 5be9806 Nov 1, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Apply fixes from StyleCI Nov 1, 2018
tests/Unit Apply fixes from StyleCI Nov 1, 2018
.coveralls.yml Initial commit Nov 1, 2018
.gitignore Initial commit Nov 1, 2018
.travis.yml Test additional PHP versions Nov 1, 2018
LICENSE Create LICENSE Nov 1, 2018
README.md Various readme typos Nov 1, 2018
composer.json Typo Nov 1, 2018
phpunit.xml Initial commit Nov 1, 2018

README.md

✖️ Math Eval Helper Function

Build Status Coverage Status StyleCI Packagist

This PHP package provides a math_eval helper function, that allows allows safe evaluation mathematical expressions, without the use of the potentially dangerous eval function.

Installation

The math_eval package can be easily installed using Composer. Just run the following command from the root of your project.

composer require "langleyfoxall/math_eval"

If you have never used the Composer dependency manager before, head to the Composer website for more information on how to get started.

Usage

To evaluate a basic mathematical expression, just pass its string representation to the math_eval function.

Here are some basic usage examples:

$two = math_eval('1 + 1');
$three = math_eval('5 - 2');
$ten = math_eval('2 * 5');
$four = math_eval('8 / 2');

It is also possible to pass expression variables. See the examples below.

$ten = math_eval('a + b', ['a' => 7, 'b' => 3]);
$fifteen = math_eval('x * y', ['x' => 3, 'y' => 5]);

Credit

This package makes use of the mossadal/math-parser package.