-
Notifications
You must be signed in to change notification settings - Fork 1
Variable
A variable is a placeholder for a value within an expression. A valid variable name conforms to PHP's variable notation, excluding the prefixed dollar sign:
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
A variable must be assigned either an int
or a float
type value. Unlike constants, a variable may be defined in an expression without any registration beforehand. As variables follow the exact notation as constants, it is not possible to define pi
, for example, as a variable.
$expression = $parser->parse("x + pi");
$expression->evaluate(["x" => 2, "pi" => 1]); // 5.1415926535898, not 3
Expression::findVariables()
lists out variables occurring within an expression.
$expression = $parser->parse("x * x + z * z");
$variables = iterator_to_array($expression->findVariables()); // ["x", "x", "z", "z"]
Try out arithmexp
on the demo site!
1.1. Installation
2.1. Constant
2.2. Function
2.3. Macro
2.4. Operator
2.5. Variable
2.6. Expression Optimization
2.7. Implementation Internals