Why does this twig expression behave strangely?

I just run weird behavior in a branch.

I just used for()an array that does not have any keys installed, except the one that was added to the array before. For test cases, just assume this php array is:

$array = array (
    0 => 'test1',
    1 => 'test2',
    'someKey' => 'test3'
)

So, I did this in a branch:

{% for key, value in array %}
    {% if key == 'someKey' %}
        {{ 'something special happend' }}
    {% else %}
        {{ 'how boring' }}
    {% endif %}
{% endfor %}

What completely engulfed me was the fact that it something special happendwas named twice .

After some searching, I found that this case also applies to the key 0.

So, I tried the following:

{{ dump (key == 'someKey') }}

The result was

  • true
  • false
  • True

So, I did another test:

{{ dump(key|lower == 'somekey') }}

The result was

  • false
  • false
  • True

Since I wanted to be sure, this was my last test.

{{ dump(0 == 'somekey') }}
{{ dump(0|lower == 'somekey') }}

The result was

  • true
  • false

Why does this behavior look like this? I can not explain it.

PS: , , , , , .

0

All Articles