Open-source PHP math library

Mathematics for PHP,
made predictable.

Evaluate readable expressions with explicit grammar, stable errors, and configurable limits. Add private explanation, visual, or unit layers only when your product needs them.

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 →
Optional extensions

Keep the engine free.
Add the understanding.

MathPHP stays a small, open core. Private add-ons plug into the same contracts when your product needs to teach a calculation, present it beautifully, or keep measurements honest.

01Private package

mathphp-explaining

Turn an expression into a clear, ordered lesson: substitutions, operation rules, partial results, source spans, and translated messages.

  • Step-by-step evaluation
  • English and Danish catalogs
  • Observer-based, deterministic output
Explore package →
02Private package

mathphp-visuals

Turn formulas and analysis into renderer-neutral data with accessible SVG fallbacks for charts, graphs, matrices, and calculus.

  • Plots, areas, roots, and histograms
  • SVG and image-ready data URIs
  • Bring your own frontend renderer
Explore package →
03Private package

mathphp-units

Evaluate quantities such as 2m * 6 + 200cm with compatible conversions, dimensions, and stable errors.

  • Length, mass, time, and temperature
  • Explicit to conversions
  • Normalized and display values
Explore package →
Private packages · separate installation · distribution model to be chosenRead distribution notes →
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)');
A clearer mental model
01

Parse

Input becomes a small, immutable syntax tree. Operators and grouping are resolved before any value is calculated.

02

Validate

Names, function arity, domains, spans, and resource limits are checked before work can escape your boundary.

03

Evaluate

The result is a native integer or float. Optional packages can observe the same events and add explanations or visual models.