<p>If you want to set entry to a new value if <code>entry.title</code> is undefined or 0 length, try this:</p>

<pre><code>{% if !(entry is defined and entry.title is defined and entry.title|length) %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-06-27T05:36:31.423Z
2014-06-27T05:36:31.423Z
Marion Newlevant
<p>Since Craft 2.6 you can do this cleanly with the <code>??</code> null-coalescing operator:</p>

<pre><code>{% if not (entry.title ?? false) %}
  ...
</code></pre>

CC BY-SA 3.0
2017-02-17T03:39:18.953Z
2017-02-17T03:39:18.953Z
Marion Newlevant
<p>I've had luck using "is defined" before using other logic operators.</p>

<p>This should do it:</p>

<pre><code>{% if entry.title is defined and entry.title | length == 0 %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-06-27T05:36:28.390Z
I think you want to set `entry` to the new value if `entry.title` is not defined or is 0 length. Which is not what this code does.
Marion Newlevant
CC BY-SA 3.0
2014-06-27T14:01:41.013Z
You're right. I ended up using something slightly different in practice.
Clive Portman
CC BY-SA 3.0
2014-06-26T06:10:11.430Z
2014-06-26T06:10:11.430Z
Coppa
<p>With dev mode on, I'm getting a warning there is no entry.title, which I know about already because that's what I'm testing for:</p>

<pre><code>{% if entry.title | length == 0 %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}
</code></pre>

<p>Is there a way to test for a variable without dev mode triggering this notice?</p>

<conditional><variable><devmode>
Avoid notice of variable's not being set within dev mode
# Avoid notice of variable's not being set within dev mode

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

With dev mode on, I'm getting a warning there is no entry.title, which I know about already because that's what I'm testing for:
{% if entry.title | length == 0 %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}

Is there a way to test for a variable without dev mode triggering this notice?

**Tags:** conditional, variable, devmode

---

## 3 answers

### Answer by username_2 on 2014-06-26. Score: 12 | Accepted answer

I've had luck using "is defined" before using other logic operators.
This should do it:
{% if entry.title is defined and entry.title | length == 0 %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}

> **Comments:**
> **username_1**: I think you want to set `entry` to the new value if `entry.title` is not defined or is 0 length. Which is not what this code does.  
> **username_0**: You're right. I ended up using something slightly different in practice.  

---

### Answer by username_1 on 2014-06-27. Score: 5

If you want to set entry to a new value if entry.title is undefined or 0 length, try this:
{% if !(entry is defined and entry.title is defined and entry.title|length) %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}


---

### Answer by username_1 on 2017-02-17. Score: 3

Since Craft 2.6 you can do this cleanly with the ?? null-coalescing operator:
{% if not (entry.title ?? false) %}
  ...


---

