3

Question heading already asks it:

In a Twig template {{0 in ['a', 'b', '99']}} prints 1. WHY? I have '0' as a value and I can't check it against arrays, as that value always pops up as existing. And at the end of the day: How do I achieve the goal of checking zero against the array of strings, in Twig?

Shahriyar Imanov
  • 1,630
  • 1
  • 15
  • 27

1 Answers1

1

PHP's type coercion for comparisons goes to integers when necessary. You're checking if an integer is in the array. (int) 'a' is coerced to 0 for this comparison. So 0 is seen as being in the array.

To avoid this, you can use in_array with the strict option:

in_array('0', ['a', 'b', '99'], true)
Matt S
  • 14,406
  • 4
  • 52
  • 75