Skip to content

Constant

Muqsit Rayyan edited this page Jul 12, 2024 · 6 revisions

A constant is an identifier that is mapped to a numeric or a boolean value. All constants are case-sensitive and their value is immutable, i.e., their value cannot be changed after an expression has been parsed (Parser::parse()). A valid constant name conforms to PHP's constant expression definition:

A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Constant Definition

A constant may be registered for a given parser before expressions are parsed by invoking ConstantRegistry::registerLabel().

$registry = $parser->constant_registry;
$registry->registerLabel("myconstant", 10);

$parser->parse("myconstant * 2")->evaluate(); // 20
$parser->parse("min(myconstant, 12)")->evaluate(); // 10

A constant must be mapped to a value of the type int|float|bool). Unlike functions, resolving a constant's value does not involve any computation.

List of pre-defined constants

Constant Value
e 2.718281828459
euler 0.57721566490153
false false
inf INF
log2e 1.442695040889
log10e 0.43429448190325
ln2 0.69314718055995
ln10 2.302585092994
lnpi 1.1447298858494
pi 3.1415926535898
nan NAN
pi2 1.5707963267949
pi4 0.78539816339745
m_1pi 0.31830988618379
m_2pi 0.63661977236758
m_2sqrtpi 1.1283791670955
sqrtpi 1.7724538509055
sqrt2 1.4142135623731
sqrt3 1.7320508075689
sqrt12 0.70710678118655
true true

1. Quick Start

1.1. Installation

2. Documentation Notes

2.1. Constant
2.2. Function
2.3. Macro
2.4. Operator
2.5. Variable
2.6. Expression Optimization
2.7. Implementation Internals

Clone this wiki locally