|
| 1 | +.. index:: |
| 2 | + single: Expressions |
| 3 | + Single: Components; Expression Language |
| 4 | + |
| 5 | +The ExpressionLanguage Component |
| 6 | +================================= |
| 7 | + |
| 8 | + The ExpressionLanguage component provides an engine that can compile and |
| 9 | + evaluate expressions. An expression is a one-liner that returns a value |
| 10 | + (mostly, but not limited to, Booleans). |
| 11 | + |
| 12 | +.. versionadded:: 2.4 |
| 13 | + The ExpressionLanguage component was new in Symfony 2.4. |
| 14 | + |
| 15 | +Installation |
| 16 | +------------ |
| 17 | + |
| 18 | +You can install the component in 2 different ways: |
| 19 | + |
| 20 | +* Use the official Git repository (https://github.com/symfony/expression-language); |
| 21 | +* :doc:`Install it via Composer </components/using_components>` (``symfony/expression-language`` on `Packagist`_). |
| 22 | + |
| 23 | +Usage |
| 24 | +----- |
| 25 | + |
| 26 | +The ExpressionLanguage component can compile and evaluate expressions. |
| 27 | +Expressions are one-liners which most of the time return a boolean, you can |
| 28 | +compare them to the expression in an ``if`` statement. A simple example of an |
| 29 | +expression is ``1 + 2``. You can also use more complicated expressions, such |
| 30 | +as ``someArray[3].someMethod('bar')``. |
| 31 | + |
| 32 | +The component provides 2 ways to work with expressions: |
| 33 | + |
| 34 | +* **compile**: the expression is compiled to PHP, so it can be cached and |
| 35 | + evaluated; |
| 36 | +* **evaluation**: the expression is evaluated without being compiled to PHP. |
| 37 | + |
| 38 | +The main class of the component is |
| 39 | +:class:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage`:: |
| 40 | + |
| 41 | + use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 42 | + |
| 43 | + $language = new ExpressionLanguage(); |
| 44 | + |
| 45 | + echo $language->evaluate('1 + 2'); // displays 3 |
| 46 | + |
| 47 | + echo $language->compile('1 + 2'); // displays (1 + 2) |
| 48 | + |
| 49 | +Expression Syntax |
| 50 | +----------------- |
| 51 | + |
| 52 | +See ":doc:`/components/expression_language/syntax`" to learn the syntax of the |
| 53 | +ExpressionLanguage component. |
| 54 | + |
| 55 | +.. _Packagist: https://packagist.org/packages/symfony/expression-language |
0 commit comments