Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:52:07.363Z
Answers.Text: Hey dude, sorry for simultaneously posting! I should probably ease up a bit and give others a chance to answer a few questions. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:54:11.010Z
Answers.Text: No worries bro,  you answer is much better: ) Keep up the good work!
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:56:13.103Z
Answers.Text: @LindseyD I agree! Give others (me) a chance. In fact, please start emailing me answers.
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:38:55.260Z
Answers.LastActivityDate: 2014-06-21T02:49:51.407Z
Answers.LastEditDate: 2014-06-21T02:49:51.407Z
Answers.UserDisplayName: Selvin Ortiz
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:40:10.953Z
Answers.Text: Now I understand ))
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T21:40:28.010Z
Answers.Text: You're a machine, Lindsey.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T21:45:00.520Z
Answers.Text: Apparently there's some merit to this "gamification" concept. ;)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-21T05:42:44.560Z
Answers.Text: 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.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-21T05:47:29.650Z
Answers.Text: 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!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-20T20:31:32.077Z
Answers.LastActivityDate: 2014-06-21T06:08:07.933Z
Answers.LastEditDate: 2017-05-23T12:38:53.957Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Tags: <templating><null><defined>
Title: What's the difference between "is defined" and "is not null"?
ThreadText: # 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](https://stackoverflow.com/questions/3803282/check-if-value-isset-and-null) 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, username_2.  
  > **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 @username_5, [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**: @username_2 I agree! Give others (me) a chance. In fact, please start emailing me answers.  
  
  ---
  
  