Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

426
Visualizações
Is identical (===) in Twig

Here's the PHP code:

if ($var===0) {do something}

It "does something" only when $var is actually 0 (and if $var is not set, it doesn't work, so everything is OK).

However, Twig doesn't support === operator, and if I write:

{% if var==0 %}do something{% endif %}

it "does something" all the time (even when $var is not set). In order to fix it, I wrote such a code:

{% if var matches 0 %}do something{% endif %}

Is it a proper way to do === comparison in Twig, or I did something wrong here? If it's wrong, how should it be fixed?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to use same as in Twig for === comparisons:

{% set var1=0 %}
{% set var2='0' %}

{% if var1 is same as( 0 ) %}
    var1 is 0.
{% else %}
    var1 is not zero.
{% endif %}

{% if var2 is same as( 0 ) %}
    var2 is 0.
{% else %}
    var2 is not 0.
{% endif %}

{% if var2 is same as( '0' ) %}
    var2 is '0'.
{% else %}
    var2 is not '0'.
{% endif %}

Here is a twigfiddle showing it in operation:

https://twigfiddle.com/k09myb

Here is the documentation for same as also stating that it is equivalent to ===. Hope that helps you!

about 4 years ago · Santiago Trujillo Relatório

0

Twig doesn't have === but it has same as instead. See: https://twig.sensiolabs.org/doc/2.x/tests/sameas.html

So you could write:

{% if var is same as(0) %}do something{% endif %}

Eventually, you can use is defined to check whether the variable is set.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda