Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of PHP 5.3 there are two ways to <a href="http://php.net/language.constants.syntax" rel="noreferrer">define constants</a>: Either using the <code>const</code> keyword or using the <a href="http://php.net/define" rel="noreferrer"><code>define()</code></a> function:</p> <pre><code>const FOO = 'BAR'; define('FOO', 'BAR'); </code></pre> <p>The fundamental difference between those two ways is that <code>const</code> defines constants at compile time, whereas <code>define</code> defines them at run time. This causes most of <code>const</code>'s disadvantages. Some disadvantages of <code>const</code> are:</p> <ul> <li><p><code>const</code> cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:</p> <pre><code>if (...) { const FOO = 'BAR'; // invalid } // but if (...) { define('FOO', 'BAR'); // valid } </code></pre> <p>Why would you want to do that anyways? One common application is to check whether the constant is already defined:</p> <pre><code>if (!defined('FOO')) { define('FOO', 'BAR'); } </code></pre></li> <li><p><code>const</code> accepts a static scalar (number, string or other constant like <code>true</code>, <code>false</code>, <code>null</code>, <code>__FILE__</code>), whereas <code>define()</code> takes any expression. Since PHP 5.6 constant expressions are allowed in <code>const</code> as well:</p> <pre><code>const BIT_5 = 1 &lt;&lt; 5; // valid since PHP 5.6, invalid previously define('BIT_5', 1 &lt;&lt; 5); // always valid </code></pre></li> <li><p><code>const</code> takes a plain constant name, whereas <code>define()</code> accepts any expression as name. This allows to do things like this:</p> <pre><code>for ($i = 0; $i &lt; 32; ++$i) { define('BIT_' . $i, 1 &lt;&lt; $i); } </code></pre></li> <li><p><code>const</code>s are always case sensitive, whereas <code>define()</code> allows you to define case insensitive constants by passing <code>true</code> as the third argument:</p> <pre><code>define('FOO', 'BAR', true); echo FOO; // BAR echo foo; // BAR </code></pre></li> </ul> <p>So, that was the bad side of things. Now let's look at the reason why I personally always use <code>const</code> unless one of the above situations occurs:</p> <ul> <li><code>const</code> simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.</li> <li><code>const</code>, being a language construct, can be statically analysed by automated tooling.</li> <li><p><code>const</code> defines a constant in the current namespace, while <code>define()</code> has to be passed the full namespace name:</p> <pre><code>namespace A\B\C; // To define the constant A\B\C\FOO: const FOO = 'BAR'; define('A\B\C\FOO', 'BAR'); </code></pre></li> <li><p>Since PHP 5.6 <code>const</code> constants can also be arrays, while <code>define()</code> does not support arrays yet. However arrays will be supported for both cases in PHP 7.</p> <pre><code>const FOO = [1, 2, 3]; // valid in PHP 5.6 define('FOO', [1, 2, 3]); // invalid in PHP 5.6, valid in PHP 7.0 </code></pre></li> </ul> <p>Finally, note that <code>const</code> can also be used within a class or interface to define a <a href="http://php.net/manual/en/language.oop5.constants.php" rel="noreferrer">class constant</a> or interface constant. <code>define</code> cannot be used for this purpose:</p> <pre><code>class Foo { const BAR = 2; // valid } // but class Baz { define('QUX', 2); // invalid } </code></pre> <p><strong>Summary</strong></p> <p>Unless you need any type of conditional or expressional definition, use <code>const</code>s instead of <code>define()</code>s - simply for the sake of readability!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload