<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.define.php',
    1 => 'define',
    2 => 'Defines a named constant',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'Misc. Functions',
  ),
  'prev' => 
  array (
    0 => 'function.constant.php',
    1 => 'constant',
  ),
  'next' => 
  array (
    0 => 'function.defined.php',
    1 => 'defined',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/misc/functions/define.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.define" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">define</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">define</span> &mdash; <span class="dc-title">Defines a named constant</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.define-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>define</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$constant_name</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$case_insensitive</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Defines a named constant at runtime.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.define-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">constant_name</code></dt>
     <dd>
      <p class="para">
       The name of the constant.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        It is possible to <span class="function"><strong>define()</strong></span> constants with reserved or
        even invalid names, whose value can (only) be retrieved with
        <span class="function"><a href="function.constant.php" class="function">constant()</a></span>. However, doing so is not recommended.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       The value of the constant.
      </p>
      <div class="warning"><strong class="warning">Warning</strong>
       <p class="para">
        While it is possible to define <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> constants, it is
        not recommended and may cause unpredictable behavior.
       </p>
      </div>
     </dd>
    
    
     <dt><code class="parameter">case_insensitive</code></dt>
     <dd>
      <p class="para">
       If set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, the constant will be defined case-insensitive. 
       The default behavior is case-sensitive; i.e. 
       <code class="literal">CONSTANT</code> and <code class="literal">Constant</code> represent
       different values.
      </p>
      <div class="warning"><strong class="warning">Warning</strong>
       <p class="simpara">
        Defining case-insensitive constants is deprecated as of PHP 7.3.0.
        As of PHP 8.0.0, only <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is an acceptable value, passing
        <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will produce a warning.
       </p>
      </div>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Case-insensitive constants are stored as lower-case.
       </p>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.define-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.define-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.1.0</td>
       <td>
        <code class="parameter">value</code> can now be an object.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Passing <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> to <code class="parameter">case_insensitive</code> now emits an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>. Passing <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is still allowed.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        <code class="parameter">case_insensitive</code> has been deprecated and will be removed in version 8.0.0.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.define-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Defining Constants</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"CONSTANT"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Hello world."</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">; </span><span style="color: #FF8000">// outputs "Hello world."<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">Constant</span><span style="color: #007700">; </span><span style="color: #FF8000">// outputs "Constant" and issues a notice.<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"GREETING"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Hello you."</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">GREETING</span><span style="color: #007700">; </span><span style="color: #FF8000">// outputs "Hello you."<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">Greeting</span><span style="color: #007700">; </span><span style="color: #FF8000">// outputs "Hello you."<br /><br />// Works as of PHP 7<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'ANIMALS'</span><span style="color: #007700">, array(<br />    </span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'bird'<br /></span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">ANIMALS</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// outputs "cat"<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 Constants with Reserved Names</strong></p>
    <div class="example-contents"><p>
     This example illustrates the <em>possibility</em> to define a
     constant with the same name as a
     <a href="language.constants.magic.php" class="link">magic constant</a>.
     Since the resulting behavior is obviously confusing, it is not recommended
     to do this in practise, though.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'__LINE__'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'__LINE__'</span><span style="color: #007700">, </span><span style="color: #DD0000">'test'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'__LINE__'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__LINE__</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">bool(false)
bool(true)
string(4) &quot;test&quot;
int(5)</pre>
</div>
   </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.define-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.defined.php" class="function" rel="rdfs-seeAlso">defined()</a> - Checks whether a constant with the given name exists</span></li>
    <li><span class="function"><a href="function.constant.php" class="function" rel="rdfs-seeAlso">constant()</a> - Returns the value of a constant</span></li>
    <li>The section on <a href="language.constants.php" class="link">Constants</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>