Reference

The useful parts,
in one place.

MathPHP keeps the language intentionally small. That makes expressions easy to review and failures easy to explain.

01 · Grammar

Familiar operators, explicit behavior.

Use numbers, variables, parentheses, unary signs, and the operators below. Exponentiation is right-associative.

OperatorMeaningExample
+ -Addition / subtraction18 - 4
* / %Product / quotient / remainder9 % 4
^Exponentiation2^3^2
( )Grouping(subtotal + tax)
02 · Functions

Ten carefully chosen building blocks.

Functions are deterministic and validated before they run.

abs(x)sqrt(x)sin(x)cos(x)tan(x)log(x)log10(x)exp(x)min(a, b, ...)max(a, b, ...)
03 · Errors

Stable codes. Useful spans.

Catch MathException and expose the machine-readable code and exact source span to your UI.

lex.malformed_numberInvalid numeric token
parse.unexpected_tokenExpression structure is invalid
eval.division_by_zeroDivision or modulo by zero
eval.integer_overflowExact integer result cannot fit
04 · Limits

Bounded on purpose.

Configure resource limits for expression length, nesting depth, function arguments, and factorial inputs.

No surprises in production.
Limits fail closed with explicit error codes before work can grow without bound.
05 · PHP API

Install, evaluate, inspect.

// composer require mathphp/mathphp
use MathPHP\Math;

try {
    $result = Math::evaluate(
        '(subtotal + tax) * 1.05',
        ['subtotal' => 42.5, 'tax' => 0.2],
    );
} catch (MathException $error) {
    echo $error->getErrorCode();
}