# `if`
The `if` statement in Twig is comparable with the if statements of PHP.
In the simplest form you can use it to test if an expression evaluates to`true`:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% if online == false %}
<p>Our website is in maintenance mode. Please, come back later.</p>
{% endif %}
</pre></div></td></tr></table>
You can also test if an array is not empty:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
</pre></div></td></tr></table>
Note
If you want to test if the variable is defined, use `if users isdefined` instead.
You can also use `not` to check for values that evaluate to `false`:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% if not user.subscribed %}
<p>You are not subscribed to our mailing list.</p>
{% endif %}
</pre></div></td></tr></table>
For multiple conditions, `and` and `or` can be used:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% if temperature > 18 and temperature < 27 %}
<p>It's a nice day for a walk in the park.</p>
{% endif %}
</pre></div></td></tr></table>
For multiple branches `elseif` and `else` can be used like in PHP. You canuse more complex `expressions` there too:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
</pre></div></td></tr></table>
Note
The rules to determine if an expression is `true` or `false` are thesame as in PHP; here are the edge cases rules:
| Value | Boolean evaluation |
|-----|-----|
| empty string | false |
| numeric zero | false |
| whitespace-only string | true |
| empty array | false |
| null | false |
| non-empty array | true |
| object | true |
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as