Skip to content

Commit

Permalink
minor symfony#19711 [ExpressionLanguage] Update the docs about the nu…
Browse files Browse the repository at this point in the history
…ll-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 symfony#19695.

Commits
-------

ca016d7 [ExpressionLanguage] Update the docs about the null-coalescing operator
  • Loading branch information
javiereguiluz committed Apr 2, 2024
2 parents 2cd9cfa + ca016d7 commit 3e350e2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
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

0 comments on commit 3e350e2

Please sign in to comment.