Skip to content

Commit 6631f70

Browse files
committed
Added operators
1 parent 27822ff commit 6631f70

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

components/expression_language/introduction.rst

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,115 @@ You can install the component in 2 different ways:
2020
* Use the official Git repository (https://github.com/symfony/ExpressionLanguage);
2121
* :doc:`Install it via Composer </components/using_components>` (``symfony/expression-language`` on `Packagist`_).
2222

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 provide 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+
Supported Literals
50+
~~~~~~~~~~~~~~~~~~
51+
52+
The component supports:
53+
54+
* **strings** - single and double quotes (e.g. ``'hello'``)
55+
* **numbers** - e.g. ``103``
56+
* **arrays** - using twig notation (e.g. ``[1, 2]``)
57+
* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``)
58+
* **booleans** - ``true`` and ``false``
59+
* **null** - ``null``
60+
61+
Supported Operators
62+
~~~~~~~~~~~~~~~~~~~
63+
64+
The component comes with a lot of operators:
65+
66+
Arithmetic Operators
67+
....................
68+
69+
* ``+`` (addition)
70+
* ``-`` (subtraction)
71+
* ``*`` (multiplication)
72+
* ``/`` (division)
73+
* ``%`` (modulus)
74+
* ``**`` (pow)
75+
76+
Assignment Operators
77+
....................
78+
79+
* ``=``
80+
81+
Bitwise Operators
82+
.................
83+
84+
* ``&`` (and)
85+
* ``|`` (or)
86+
* ``^`` (xor)
87+
88+
Comparison Operators
89+
....................
90+
91+
* ``==`` (equal)
92+
* ``===`` (identical)
93+
* ``!=`` (not equal)
94+
* ``!==`` (not identical)
95+
* ``<`` (less than)
96+
* ``>`` (greater than)
97+
* ``<=`` (less than or equal to)
98+
* ``>=`` (greater than or equal to)
99+
* ``=~`` (regex match)
100+
* ``!~`` (regex does not match)
101+
102+
.. sidebar:: Regex Operator
103+
104+
The Regex Operators (``=~`` and ``!~``) are coming from Perl. This
105+
operator matches if the regular expression on the right side of the
106+
operator matches the string on the left. For instance,
107+
``'foobar' =~ '/foo/'`` evaluates to true.
108+
``!~`` is the opposite and matches if the regular expression does *not*
109+
match the string.
110+
111+
Logical Operators
112+
.................
113+
114+
* ``not`` or ``!``
115+
* ``and`` or ``&&``
116+
* ``or`` or ``||``
117+
118+
String Operators
119+
................
120+
121+
* ``~`` (concatenation)
122+
123+
Array Operators
124+
...............
125+
126+
* ``in`` (contain)
127+
* ``not in`` (does not contain)
128+
129+
Numeric Operators
130+
.................
131+
132+
* ``..`` (range)
133+
23134
.. _Packagist: https://packagist.org/packages/symfony/expression-language

0 commit comments

Comments
 (0)