[Doc] Document loose comparison in the in operator

Clarifies that `in` performs a loose comparison on sequences, mappings,
and `Traversable` objects (similar to `==`), points to `same as` for
strict comparisons, and warns about boolean left operands.

Refs #4650
This commit is contained in:
Pascal CESCON - Amoifr
2026-05-08 18:35:48 +02:00
parent 99d95dd6b8
commit 8af8e93970
+15
View File
@@ -794,6 +794,21 @@ operand is contained in the right:
You can use this operator to perform a containment test on strings,
sequences, mappings, or objects implementing the ``Traversable`` interface.
.. note::
For sequences, mappings, and ``Traversable`` objects, ``in`` uses a loose
comparison (similar to ``==``); use :doc:`same as <tests/sameas>` for a
strict comparison. Like PHP's ``in_array()``, this can yield unexpected
results when the left operand is a boolean:
.. code-block:: twig
{# returns true because true == 'foo' under PHP loose comparison #}
{{ true in ['foo', 'bar'] }}
Containment on strings only accepts string, integer, and float operands on
the left; other types always return ``false``.
To perform a negative test, use the ``not in`` operator:
.. code-block:: twig