PHPackages                             fintara/calculator-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fintara/calculator-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fintara/calculator-php
======================

Calculator for arithmetic expressions

2.0.1(4y ago)1331.2k↓100%3[1 issues](https://github.com/fintara/calculator-php/issues)MITPHPPHP &gt;=7.1

Since Feb 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/fintara/calculator-php)[ Packagist](https://packagist.org/packages/fintara/calculator-php)[ RSS](/packages/fintara-calculator-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (7)Used By (0)

PHP Calculator for arithmetic expressions
-----------------------------------------

[](#php-calculator-for-arithmetic-expressions)

PHP calculator which evaluates different arithmetic expressions:

```
2+5.9       = 7.9
3.5^(2-5)   = 0.02332361516035
5-(2*(1+2)) = -1

```

The included functions by default are `sqrt(x)` and `log(base, arg)`, but there is also an option to add custom functions with any number of arguments.

### Basic usage

[](#basic-usage)

```
use \Fintara\Tools\Calculator\Calculator;

$calculator = Calculator::create(); // use default tokenizer
echo $calculator->calculate('1+2*3/4'); // 2.5
```

### Advanced usage

[](#advanced-usage)

#### Custom functions

[](#custom-functions)

You can add custom functions:

- `name`: name of the function, as it will be used in expressions. All lower case and \_ (underscore) allowed.
- `implementation`: how to evaluate the function.

```
$calculator->addFunction('cbrt', function($x) {
    return pow($x, 1/3);
});

echo $calculator->calculate('cbrt(27)'); // 3
```

#### Tokenizer

[](#tokenizer)

You can also use the tokenizer (or supply calculator with your own):

```
$tokenizer = new Tokenizer();
$tokens = $tokenizer->tokenize('1+2*3.5'); // [1, '+', 2, '*', 3.5]
```

In case the expression contains functions (e.g. `sqrt(x)`), the tokenizer needs to know all functions' names as second parameter.

```
$tokens = $tokenizer->tokenize('1+sqrt(4)', ['sqrt']); // [1, '+', 'sqrt', '(', 4, ')']
```

**Note:** The default tokenizer automatically adds `*` (multiplication) sign between a number and following function or a number and following parenthesis (if the sign is not found).

```
$tokens = $tokenizer->tokenize('2 (1 + 3)'); // [2, '*', '(', 1, '+', 3, ')']
```

### Changelog

[](#changelog)

2.0.1

- Added LICENSE

2.0.0

- Breaking: operator `mod` is renamed to `%`.
- Breaking: `addFunction` does not need number of arguments anymore (removed 3rd argument).
- Breaking: adding function with existing name throws. You can use the helper `replaceFunction`.
- Breaking: `getReversePolishNotation` is private.
- Breaking: `setExpression` is removed. Use directly `calculate($expression)`.
- Breaking: `ILexer` and `DefaultLexer` are replaced with `TokenizerInterface` and `Tokenizer` respectively.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~991 days

Total

4

Last Release

1488d ago

Major Versions

v0.1 → 2.0.02017-09-06

PHP version history (2 changes)v0.1-alphaPHP &gt;=5.4

2.0.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/a166a27611a389942728bad72a55a67480c9395de7d06c4bc28d90206a99e282?d=identicon)[fintara](/maintainers/fintara)

---

Top Contributors

[![fintara](https://avatars.githubusercontent.com/u/4290594?v=4)](https://github.com/fintara "fintara (34 commits)")

---

Tags

arithmetic-expressionarithmeticscalculatormathematicsphpphp-calculator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fintara-calculator-php/health.svg)

```
[![Health](https://phpackages.com/badges/fintara-calculator-php/health.svg)](https://phpackages.com/packages/fintara-calculator-php)
```

###  Alternatives

[anlutro/l4-settings

Persistent settings in Laravel.

9312.4M19](/packages/anlutro-l4-settings)[godruoyi/php-snowflake

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

8582.3M61](/packages/godruoyi-php-snowflake)[tuupola/ksuid

K-Sortable Globally Unique IDs

1081.2M4](/packages/tuupola-ksuid)[mcordingley/linearalgebra

Matrix math for PHP.

85146.3k1](/packages/mcordingley-linearalgebra)[hyva-themes/magento2-wysiwyg-svg

Allow SVGs and all tailwind classes in CMS block and page content.

17130.8k](/packages/hyva-themes-magento2-wysiwyg-svg)[syrian-open-source/laravel-youtube-iframe-generator

Laravel package allows you to generate an iframe tag with a video player depending on a youtube URL.

184.2k](/packages/syrian-open-source-laravel-youtube-iframe-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
