<p>The first statement checks for the existence of <code>var</code>, the second checks that it has a value other than <code>null</code> and will throw and error if <code>var</code> does not exist.</p>

<p>If you're not sure whether a variable exists, you should check if it exists first then check for its value to be safe.</p>

<p>You could combine both statements like so <code>{% if var is defined and var is not null %}</code> or use <code>empty, not empty</code> if you're feeling fancy: )</p>

CC BY-SA 3.0
2014-06-20T20:52:07.363Z
Hey dude, sorry for simultaneously posting! I should probably ease up a bit and give others a chance to answer a few questions. :)
Lindsey D
CC BY-SA 3.0
2014-06-20T20:54:11.010Z
No worries bro,  you answer is much better: ) Keep up the good work!
Selvin Ortiz
CC BY-SA 3.0
2014-06-20T20:56:13.103Z
@LindseyD I agree! Give others (me) a chance. In fact, please start emailing me answers.
Matt Stein
CC BY-SA 3.0
2014-06-20T20:38:55.260Z
2014-06-21T02:49:51.407Z
2014-06-21T02:49:51.407Z
Selvin Ortiz
<p><code>is defined</code> - The variable has been defined, and set to any value (or <code>null</code>).</p>

<p><code>is not null</code> - The variable has been defined, and is specifically <strong>not</strong> <code>null</code>.</p>

<p>In PHP terms, it's like the difference between <code>is_null()</code> vs. checking whether the variable exists using <code>get_defined_vars()</code> (see this <a href="https://stackoverflow.com/questions/3803282/check-if-value-isset-and-null">Stack Overflow thread</a> for a little more clarity)</p>

<hr>

<p>Try this experiment... Without setting <code>myVar</code>, put this in your template:</p>

<pre><code>{{ (myVar is defined) }}
</code></pre>

<p>Since that equates to <code>false</code>, nothing will appear in your template. Now change it to this:</p>

<pre><code>{{ (myVar is not null) }}
</code></pre>

<p>That will actually throw an error, since <code>myVar</code> was never defined.</p>

CC BY-SA 3.0
2014-06-20T20:40:10.953Z
Now I understand ))
nicael
CC BY-SA 3.0
2014-06-20T21:40:28.010Z
You're a machine, Lindsey.
Brad Bell
CC BY-SA 3.0
2014-06-20T21:45:00.520Z
Apparently there's some merit to this "gamification" concept. ;)
Lindsey D
CC BY-SA 3.0
2014-06-21T05:42:44.560Z
Technically `is null` is a test, too. Anything starting with “is” or “is not” is a [test](http://twig.sensiolabs.org/doc/tests/index.html). `is null` is just testing for a null value, whereas `is defined` is testing for whether the value exists at all.
Brandon Kelly
CC BY-SA 3.0
2014-06-21T05:47:29.650Z
Very good point @BrandonKelly, [I actually learned that](http://craftcms.stackexchange.com/questions/351/how-can-i-determine-if-a-user-is-logged-in-and-who-that-user-is/352?noredirect=1#comment603_352) a few hours after I posted this answer. I'll go ahead and update my answer. Thanks!
Lindsey D
CC BY-SA 3.0
2014-06-20T20:31:32.077Z
2014-06-21T06:08:07.933Z
2017-05-23T12:38:53.957Z
Lindsey D
<pre><code>{% if var is defined %}
</code></pre>

<p>and</p>

<pre><code>{% if var is not null %}
</code></pre>

<p>Is there a difference in usage of them?</p>

<templating><null><defined>
What's the difference between "is defined" and "is not null"?
# What's the difference between "is defined" and "is not null"?

(Asked by: username_0 on 2014-06-20)

{% if var is defined %}

and
{% if var is not null %}

Is there a difference in usage of them?

**Tags:** templating, null, defined


---

## 2 answers

### Answer by username_2 on 2014-06-20. Score: 14 | Accepted answer

is defined - The variable has been defined, and set to any value (or null).
is not null - The variable has been defined, and is specifically not null.
In PHP terms, it's like the difference between is_null() vs. checking whether the variable exists using get_defined_vars() (see this Stack Overflow thread for a little more clarity)

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 not null) }}

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

> **Comments:**
> **username_0**: Now I understand ))  
> **username_4**: You're a machine, Lindsey.  
> **username_2**: Apparently there's some merit to this "gamification" concept. ;)  
> **username_5**: Technically `is null` is a test, too. Anything starting with “is” or “is not” is a [test](http://twig.sensiolabs.org/doc/tests/index.html). `is null` is just testing for a null value, whereas `is defined` is testing for whether the value exists at all.  
> **username_2**: Very good point @BrandonKelly, [I actually learned that](http://craftcms.stackexchange.com/questions/351/how-can-i-determine-if-a-user-is-logged-in-and-who-that-user-is/352?noredirect=1#comment603_352) a few hours after I posted this answer. I'll go ahead and update my answer. Thanks!  

---

### Answer by username_1 on 2014-06-20. Score: 6

The first statement checks for the existence of `var`, the second checks that it has a value other than `null` and will throw and error if `var` does not exist.

If you're not sure whether a variable exists, you should check if it exists first then check for its value to be safe.

You could combine both statements like so `{% if var is defined and var is not null %}` or use `empty, not empty` if you're feeling fancy: )

> **Comments:**
> **username_2**: Hey dude, sorry for simultaneously posting! I should probably ease up a bit and give others a chance to answer a few questions. :)  
> **username_1**: No worries bro,  you answer is much better: ) Keep up the good work!  
> **username_3**: @LindseyD I agree! Give others (me) a chance. In fact, please start emailing me answers.  

---

