Skip to content

Commit 3e350e2

Browse files
committed
minor #19711 [ExpressionLanguage] Update the docs about the null-coalescing operator (javiereguiluz)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [ExpressionLanguage] Update the docs about the null-coalescing operator Fixes #19695. Commits ------- ca016d7 [ExpressionLanguage] Update the docs about the null-coalescing operator
2 parents 2cd9cfa + ca016d7 commit 3e350e2

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

components/expression_language.rst

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,10 @@ The main class of the component is
8080
Null Coalescing Operator
8181
........................
8282

83-
This is the same as the PHP `null-coalescing operator`_, which combines
84-
the ternary operator and ``isset()``. It returns the left hand-side if it exists
85-
and it's not ``null``; otherwise it returns the right hand-side. Note that you
86-
can chain multiple coalescing operators.
87-
88-
* ``foo ?? 'no'``
89-
* ``foo.baz ?? 'no'``
90-
* ``foo[3] ?? 'no'``
91-
* ``foo.baz ?? foo['baz'] ?? 'no'``
92-
93-
.. versionadded:: 6.2
83+
.. note::
9484

95-
The null-coalescing operator was introduced in Symfony 6.2.
85+
This content has been moved to the ref:`null coalescing operator <component-expression-null-coalescing-operator>`
86+
section of ExpressionLanguage syntax reference page.
9687

9788
Parsing and Linting Expressions
9889
...............................

reference/formats/expression_language.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ JavaScript::
9999

100100
This will print out ``Hi Hi Hi!``.
101101

102+
.. _component-expression-null-safe-operator:
103+
102104
Null-safe Operator
103105
..................
104106

@@ -118,6 +120,29 @@ operator)::
118120

119121
The null safe operator was introduced in Symfony 6.1.
120122

123+
.. _component-expression-null-coalescing-operator:
124+
125+
Null-Coalescing Operator
126+
........................
127+
128+
It returns the left-hand side if it exists and it's not ``null``; otherwise it
129+
returns the right-hand side. Expressions can chain multiple coalescing operators:
130+
131+
* ``foo ?? 'no'``
132+
* ``foo.baz ?? 'no'``
133+
* ``foo[3] ?? 'no'``
134+
* ``foo.baz ?? foo['baz'] ?? 'no'``
135+
136+
.. note::
137+
138+
The main difference with the `null-coalescing operator in PHP`_ is that
139+
ExpressionLanguage will throw an exception when trying to access a
140+
non-existent variable.
141+
142+
.. versionadded:: 6.2
143+
144+
The null-coalescing operator was introduced in Symfony 6.2.
145+
121146
.. _component-expression-functions:
122147

123148
Working with Functions
@@ -391,6 +416,12 @@ Ternary Operators
391416
* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``)
392417
* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``)
393418

419+
Other Operators
420+
~~~~~~~~~~~~~~~
421+
422+
* ``?.`` (:ref:`null-safe operator <component-expression-null-safe-operator>`)
423+
* ``??`` (:ref:`null-coalescing operator <component-expression-null-coalescing-operator>`)
424+
394425
Built-in Objects and Variables
395426
------------------------------
396427

@@ -401,3 +432,5 @@ expressions (e.g. the request, the current user, etc.):
401432
* :doc:`Variables available in security expressions </security/expressions>`;
402433
* :doc:`Variables available in service container expressions </service_container/expression_language>`;
403434
* :ref:`Variables available in routing expressions <routing-matching-expressions>`.
435+
436+
.. _`null-coalescing operator in PHP`: https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

0 commit comments

Comments
 (0)