nxp/math-executor
Safely parse and evaluate math expressions from strings in PHP. nxp/math-executor is a lightweight library with variables, functions, custom operators, and caching, ideal for calculators, dynamic pricing, and formula-driven logic in Laravel and PHP apps.
eval()) from the design conversation entirely.FormulaEvaluator port) and a pinned version, not for building product surface directly against its API. The decision it supports is "adopt cheaply, insulate deliberately."MathExecutor exceptions instead of leaking native PHP errors (TypeError, DivisionByZeroError, etc.). For the PM this is the difference between "formula validation is a nice-to-have" and "we can surface a specific, user-facing validation message on save and on preview." It makes a preview/validate-before-publish step a credible v1 requirement for the rules UI rather than a later hardening item, and it means support tickets read "your formula has an invalid value in step 3" rather than a 500.Adopt when most of these hold:
shipping_cost(), tax_rate()) and custom operators, and want to register them in PHP rather than build a language.eval()/create_function-style shortcuts from the codebase for security review.Look elsewhere when any of these hold:
symfony/expression-language is the safer default: larger community, mature sandbox, stricter parsing, and a well-understood security story. Paying the extra integration cost is cheaper than owning a security incident.brick/math or keep decimals as integers and use the package only for rule shape, not final amounts.PhpSpreadsheet's calculation engine is the honest comparison — otherwise you will spend quarters chasing formula-compatibility tickets.To executives: We can offer configurable pricing, scoring, and eligibility rules without putting a release behind every change. This is a small, MIT-licensed PHP library — no vendor contract, no infrastructure, no data leaving our systems — that lets the application evaluate a formula supplied as text, safely, instead of us writing and maintaining a custom calculator. The latest release (v2.4.0) also means bad formulas fail predictably and with a clear error rather than crashing, which is what lets us show a meaningful message in the admin UI instead of a page error. The business outcome is faster commercial iteration and fewer engineering escalations for "change the discount formula." The honest caveats: it is a modestly adopted open-source component with essentially one maintainer, so we will wrap it behind our own interface and pin the version, which keeps the exit cheap. I'd frame this as a two-week enabler for the rules roadmap, not a strategic platform commitment.
To engineering: It solves tokenizing, operator precedence, variables, and function dispatch, so we don't hand-roll a parser or reach for eval(). Integration is a thin service class plus a registry of our own functions; we own allow-listing of variables and functions, input limits, decimal rounding, and caching of parsed expressions per request cycle. v2.4.0 is a behavior change worth catching in review: invalid values in math operations no longer surface as native PHP exceptions but as MathExecutor exceptions. Any existing try/catch around evaluation that targets TypeError / DivisionByZeroError / Error must be re-pointed at the package's exception type (and our adapter should normalize it into a domain-level FormulaEvaluationFailed), otherwise error paths that used to be handled will start bubbling as unhandled. Add a divide-by-zero and a non-numeric-variable case to the characterization suite as part of the bump. Recommended posture otherwise unchanged: depend on it through an internal FormulaEvaluator contract so a swap to symfony/expression-language or a full rules engine is a one-class change, and add characterization tests around our formulas before we let anyone edit them in production.
The ask: approve a short spike — one engineer, a fixed set of real pricing/scoring formulas, precision and performance measured against our money-handling standard — with a go/no-go on the adapter-wrapper approach at the end. Fold the v2.4.0 exception-normalization check into that spike; it is a half-day, not a project.
How can I help you explore Laravel packages today?