Skip to main content
3 of 6
Elaborated
Lindsey D
  • 24.1k
  • 5
  • 54
  • 111

Yes, it matters.

null is a value - The variable has been defined but not set to anything.

defined means exactly what it says - The variable has been defined (to any value).

Try this experiment... Without setting myVar, put this in your template:

{{ (myVar is defined) }}

Since that equates to false, nothing will appear in your template. Now change it to this:

{{ (myVar is null) }}

That will actually throw an error, since myVar was never defined.

To further elaborate, null is a value (as previously mentioned), while defined is a test.

http://twig.sensiolabs.org/doc/tests/defined.html

In PHP terms, it's like the difference between is_null() vs. checking whether the value exists in get_defined_vars() (see this Stack Overflow thread for a little more clarity)

Lindsey D
  • 24.1k
  • 5
  • 54
  • 111