- How do I install nxp/math-executor in a Laravel app?
- Install it with Composer using composer require nxp/math-executor. It is a framework-agnostic PHP library, so no Laravel service provider or config file is required. Most teams bind it inside an application-owned service such as MathExpressionService and register allowed functions and variables there.
- Does nxp/math-executor support Laravel 11 and newer?
- The package has no Laravel-version coupling, so it works with any Laravel release that runs a compatible PHP version. Check the PHP constraint in its composer.json against your production and CI matrices before upgrading. Confirm that v2.4.0 does not require code changes beyond exception handling.
- How do I add variables and custom functions to a math expression?
- The library accepts variables and custom functions through its API, letting you inject values like price or quantity and register callbacks for domain-specific calculations. Keep the function and variable allowlist in config rather than exposing the executor directly to controllers or Blade. This keeps custom logic centralized and avoids global state leaking between requests.
- Is nxp/math-executor safe to use with user-supplied input?
- It tokenizes and evaluates expressions without using eval(), which removes the most direct code-execution risk. However, user-supplied expressions are still untrusted input and can create denial-of-service risk, so cap expression length, nesting depth, and evaluation scope. Only expose an allowlisted set of operators and functions.
- How does nxp/math-executor handle division by zero and invalid expressions?
- Version 2.4.0 throws package-specific MathExecutor exceptions instead of native PHP exceptions for invalid math values such as division by zero. Catch the package's base exception and map it to an application-owned domain error. Do not rely on catching TypeError, ValueError, or DivisionByZeroError from older code paths.
- Can I use nxp/math-executor for pricing calculations with money?
- Evaluation is float-based, so it is not suitable for exact monetary arithmetic without an explicit rounding strategy. For pricing or quoting rules, define precision and rounding at the application layer and treat the evaluator as a formula parser, not a ledger. Validate and store the final amounts using your normal monetary handling.
- Does nxp/math-executor cache parsed expressions for performance?
- The library includes caching support so parsed expressions can be reused rather than reparsed on every evaluation. Confirm how caching is configured in your version and whether it is safe to share across requests in your deployment model. For high-volume formula fields, benchmark parsing versus evaluation to decide what to cache.
- What operators and math functions are built in?
- It supports arithmetic operators, parentheses, and operator precedence, plus built-in math functions such as sin, cos, and sqrt. You can also add custom operators and functions for your own domain needs. Verify the exact operator and function list in the README before mapping existing formulas.
- What changed in nxp/math-executor v2.4.0 for exception handling?
- v2.4.0 changes the exception contract by throwing package-specific MathExecutor exceptions instead of native PHP exceptions for invalid math values. Audit existing catch blocks and tests for reliance on TypeError, ValueError, or DivisionByZeroError. Then normalize package exceptions into your own application error types.
- Is nxp/math-executor actively maintained, and what are the alternatives?
- Adoption signal is weak, with roughly 230 GitHub stars and zero dependents, and it appears to be maintainer-light despite recent v2.4.0 work. It is MIT licensed and safe for commercial use, but verify release dates and CVE history yourself. Alternatives include symfony/expression-language or a custom calculator behind an adapter interface.