Scalar expressions for PHP

Math, with boundaries.

A small, predictable expression evaluator for the moments when a calculator needs to live inside your application.

PHP 8.2+ · no runtime dependencies · explicit errors
mathphp / playground ready
> (subtotal + tax) * 1.05
subtotal = 42.50   tax = 0.20
53.55float
deterministicboundedtyped
Small surface area.Clear contracts at every edge.
Safe by default.No eval(), no hidden state.
Useful immediately.Operators, functions, variables.
Why MathPHP
01

Expressions that stay readable.

Write the calculation your users already understand. Variables, familiar operators, and a focused set of math functions.

See the grammar
02

Errors that tell the truth.

Every failure has a stable code and source span, so validation messages can be useful instead of mysterious.

Explore error codes
03

Boundaries you can trust.

Overflow, non-finite values, malformed input, and resource limits are handled explicitly and consistently.

View the limits
A tiny API

One call between input and result.

Keep the evaluator behind your own form, API, or rule editor. MathPHP does the careful part.

$result = Math::evaluate('2 * (3 + 4)');