<p><em>Hat-tip to Brad for finding this answer.</em></p>

<p>This can be achieved with something like the following in your index.php:</p>

<pre><code>define('BRADS_FAVORITE_HOBBY', "Drinking");
</code></pre>

<p>Thanks to the <a href="http://twig.sensiolabs.org/doc/functions/constant.html" rel="noreferrer">power to Twig</a>, you can then access this in your templates using:</p>

<pre><code>{{ constant('BRADS_FAVORITE_HOBBY') }}
</code></pre>

CC BY-SA 3.0
2014-11-18T10:48:58.967Z
Very useful in certain cases, indeed. Thanks for sharing, Steve.
Jérôme Coupé
CC BY-SA 3.0
2014-11-18T09:53:31.343Z
2014-11-18T09:53:31.343Z
Steve Abraham
<p>I was looking for a way to set arbitrary PHP constants in the index.php that could then be accessed within templates.</p>

<p>This is particularly useful if you have more than one index/domain pulling content from a core Craft install as the additional domains won't have any configuration files to use, just index.php.</p>

<variable><php><index>
Setting arbitrary PHP constants in index.php
# Setting arbitrary PHP constants in index.php

(Asked by: username_0 on 2014-11-18)

I was looking for a way to set arbitrary PHP constants in the index.php that could then be accessed within templates.

This is particularly useful if you have more than one index/domain pulling content from a core Craft install as the additional domains won't have any configuration files to use, just index.php.

**Tags:** variable, php, index

---

## 1 answers

### Answer by username_0 on 2014-11-18. Score: 5

Hat-tip to Brad for finding this answer.
This can be achieved with something like the following in your index.php:
define('BRADS_FAVORITE_HOBBY', "Drinking");

Thanks to the power to Twig, you can then access this in your templates using:
{{ constant('BRADS_FAVORITE_HOBBY') }}

> **Comments:**
> **username_1**: Very useful in certain cases, indeed. Thanks for sharing, Steve.  

---

