https://github.com/rodriados/mathr
PHP Package for Mathematical Execution
https://github.com/rodriados/mathr
calculator math parser php
Last synced: 3 months ago
JSON representation
PHP Package for Mathematical Execution
- Host: GitHub
- URL: https://github.com/rodriados/mathr
- Owner: rodriados
- License: mit
- Created: 2017-07-10T01:08:21.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-06T06:25:02.000Z (over 3 years ago)
- Last Synced: 2025-10-06T13:27:23.483Z (9 months ago)
- Topics: calculator, math, parser, php
- Language: PHP
- Homepage:
- Size: 204 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Mathr


[](https://travis-ci.org/rodriados/mathr)
[](https://coveralls.io/github/rodriados/mathr?branch=master)
Mathr is a fast mathematical expression parser, evaluator and calculator with some added juice.
## Usage
The simplest usage possible for Mathr is by simply sending in a math expression.
```php
evaluate("3 + 4 * 5");
echo $result; // 23
```
You also can create your own variables and functions!
```php
evaluate("v = 10");
$mathr->evaluate("fibonacci(0) = 0");
$mathr->evaluate("fibonacci(1) = 1");
$mathr->evaluate("fibonacci(x) = fibonacci(x - 1) + fibonacci(x - 2)");
$result = $mathr->evaluate("fibonacci(v)");
echo $result; // 55
```
If you want to, it's possible to bind functions to native PHP closures!
```php
set('triangle(b, h)', fn ($b, $h) => ($b * $h) / 2);
$result = $mathr->evaluate('triangle(5, 8)');
echo $result; // 20
```
There are a plenty of native functions and variables which you can use at will.
```php
evaluate("fibonacci(x) = ceil((φ ^ x - (1 - φ) ^ x) / sqrt(5))");
$result = $mathr->evaluate("fibonacci(10)");
echo $result; // 55
```
You can easily export and import your functions and variables.
```php
export(); // Exports all bound functions and variables.
$mathr->import($exported); // Imports functions and variables.
```
## Install
The recommended way to install Mathr is via [Composer](http://getcomposer.org).
```json
{
"require": {
"rodriados/mathr": "v3.0"
}
}
```