Skip to content

[ExpressionLanguage] Update the docs about the null-coalescing operator #19711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions components/expression_language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,10 @@ The main class of the component is
Null Coalescing Operator
........................

This is the same as the PHP `null-coalescing operator`_, which combines
the ternary operator and ``isset()``. It returns the left hand-side if it exists
and it's not ``null``; otherwise it returns the right hand-side. Note that you
can chain multiple coalescing operators.

* ``foo ?? 'no'``
* ``foo.baz ?? 'no'``
* ``foo[3] ?? 'no'``
* ``foo.baz ?? foo['baz'] ?? 'no'``

.. versionadded:: 6.2
.. note::

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

Parsing and Linting Expressions
...............................
Expand Down
33 changes: 33 additions & 0 deletions reference/formats/expression_language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ JavaScript::

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

.. _component-expression-null-safe-operator:

Null-safe Operator
..................

Expand All @@ -118,6 +120,29 @@ operator)::

The null safe operator was introduced in Symfony 6.1.

.. _component-expression-null-coalescing-operator:

Null-Coalescing Operator
........................

It returns the left-hand side if it exists and it's not ``null``; otherwise it
returns the right-hand side. Expressions can chain multiple coalescing operators:

* ``foo ?? 'no'``
* ``foo.baz ?? 'no'``
* ``foo[3] ?? 'no'``
* ``foo.baz ?? foo['baz'] ?? 'no'``

.. note::

The main difference with the `null-coalescing operator in PHP`_ is that
ExpressionLanguage will throw an exception when trying to access a
non-existent variable.

.. versionadded:: 6.2

The null-coalescing operator was introduced in Symfony 6.2.

.. _component-expression-functions:

Working with Functions
Expand Down Expand Up @@ -391,6 +416,12 @@ Ternary Operators
* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``)
* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``)

Other Operators
~~~~~~~~~~~~~~~

* ``?.`` (:ref:`null-safe operator <component-expression-null-safe-operator>`)
* ``??`` (:ref:`null-coalescing operator <component-expression-null-coalescing-operator>`)

Built-in Objects and Variables
------------------------------

Expand All @@ -401,3 +432,5 @@ expressions (e.g. the request, the current user, etc.):
* :doc:`Variables available in security expressions </security/expressions>`;
* :doc:`Variables available in service container expressions </service_container/expression_language>`;
* :ref:`Variables available in routing expressions <routing-matching-expressions>`.

.. _`null-coalescing operator in PHP`: https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce