{"id":632,"date":"2017-03-16T14:50:36","date_gmt":"2017-03-16T14:50:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=632"},"modified":"2017-03-29T15:59:37","modified_gmt":"2017-03-29T10:29:37","slug":"php-define-vs-const","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-define-vs-const\/","title":{"rendered":"PHP define() vs const"},"content":{"rendered":"<h4 id=\"php-define-function\"><span style=\"color: #800000;\"><strong>PHP define() Function<\/strong><\/span><\/h4>\n<p><b>Definition and <\/b><b>Usage:<\/b><\/p>\n<p>The define() function defines a constant.<\/p>\n<p><strong>Constants are much like variables, except for the following differences:<\/strong><\/p>\n<ul>\n<li>A constant&#8217;s value cannot be changed after it is set<\/li>\n<li>Constant names do not need a leading dollar sign ($)<\/li>\n<li>Constants can be accessed regardless of scope<\/li>\n<li>Constant values can only be strings and numbers<\/li>\n<\/ul>\n<p><b>Example:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>define(&quot;GREETING&quot;,&quot;Hi ! How are you ?&quot;,TRUE);<br\/>echo constant(&quot;GREETING&quot;);<br\/>?&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Syntax:<\/strong><\/p>\n<p>define(name,value,case_insensitive)<\/p>\n<table style=\"width: 823px; height: 161px;\">\n<tbody>\n<tr>\n<th>Parameter<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>Required. Specifies the name of the constant<\/td>\n<\/tr>\n<tr>\n<td>value<\/td>\n<td>Required. Specifies the value of the constant<\/td>\n<\/tr>\n<tr>\n<td>case_insensitive<\/td>\n<td>Optional. Specifies whether the constant name should be case-insensitive. Possible values: TRUE &#8211; Case insensitive FALSE &#8211; Default. Case-sensitive<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>The example below creates a constant with a\u00a0case-sensitive\u00a0name:<\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>\/\/ case-sensitive constant name<br\/>define(&quot;GREETING&quot;, &quot;Welcome to wikitechy.com!&quot;);<br\/>echo GREETING;<br\/>?&gt; <\/code><\/pre> <\/div>\n<p><strong>The example below creates a constant with a\u00a0case-insensitive\u00a0name:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>\/\/ case-insensitive constant name<br\/>define(&quot;GREETING&quot;, &quot;Welcome to wikitechy.com!&quot;, true);<br\/>echo greeting;<br\/>?&gt; <\/code><\/pre> <\/div>\n<p><strong>Constants are Global :<\/strong><\/p>\n<p>Constants are automatically global and can be used across the entire script.<\/p>\n<p>Below example we uses a constant inside a function, even if it is defined outside the function:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\"><br\/>&lt;?php<br\/>define(&quot;GREETING&quot;, &quot;Welcome to wikitechy.com!&quot;);<br\/><br\/>function myTest() {<br\/>    echo GREETING;<br\/>}<br\/> <br\/>myTest();<br\/>?&gt; <\/code><\/pre> <\/div>\n<h4 id=\"define-vs-const\"><span style=\"color: #ff6600;\">define() vs const<\/span><\/h4>\n<p><strong>As of PHP 5.3 there are two ways to define constants: <\/strong><\/p>\n<p><strong>Either using the const keyword or using the define() function:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">const CONST_VAL = &#039;CONST&#039;;<br\/>define(&#039;CONST_VAL&#039;, &#039;CONST&#039;);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>The fundamental difference between those two ways is that const defines constants at compile time, whereas define defines them at run time.<\/p>\n<p>const cannot be used to conditionally define constants. It has to be used in the outermost scope.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">if (...) {<br\/>const CONST_VAL = &#039;CONST&#039;;    \/\/ invalid<br\/>}<br\/>\/\/ but<br\/>if (...) {<br\/>define(&#039;CONST_VAL&#039;, &#039;CONST&#039;); \/\/ valid<br\/>}<\/code><\/pre> <\/div>\n<p><strong>One common application is to check whether the constant is already defined:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">if (!defined(&#039;CONST_VAL&#039;)) {<br\/>define(&#039;CONST_VAL&#039;, &#039;CONST&#039;);<br\/>}<\/code><\/pre> <\/div>\n<p><strong>const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">const BIT_5 = 1 &lt;&lt; 5; \/\/ invalid<br\/>define(\u2018BIT_5\u2032, 1 &lt;&lt; 5); \/\/ valid <\/code><\/pre> <\/div>\n<p><strong>const takes a plain constant name, whereas define() accepts any expression as name.\u00a0<\/strong><\/p>\n<p><strong>This allows to do things like this:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">for ($i = 0; $i &lt; 32; ++$i) {<br\/>define(\u2018BIT_\u2019 . $i, 1 &lt;&lt; $i);<br\/>}<\/code><\/pre> <\/div>\n<p><strong>consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">define(\u2018FOO\u2019, \u2018BAR\u2019, true);<br\/>echo FOO; \/\/ BAR<br\/>echo foo; \/\/ BAR<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Now let\u2019s look at the reason why we always use const unless one of the above situations occurs:<\/strong><\/p>\n<p>const simply reads nicer. It\u2019s a language construct instead of a function and also is consistent with how you define constants in classes.<br \/>\nAs consts are language constructs and defined at compile time they are a bit faster than define()s.<\/p>\n<p><strong>It is well known that PHP define()s are slow when using a large number of constants. <\/strong><\/p>\n<p><strong>People have even invented things like apc_load_constants() and hidef to get around this.<\/strong><\/p>\n<p>consts make the definition of constants approximately twice as fast (on development machines with XDebug turned on even more). Lookup time on the other hand does not change (as both constant types share the same lookup table): Demo.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP define() Function Definition and Usage: The define() function defines a constant. Constants are much like variables, except for the following differences: A constant&#8217;s value cannot be changed after it is set Constant names do not need a leading dollar sign ($) Constants can be accessed regardless of scope Constant values can only be strings [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1203,1205,1206,1204,1202,1200,1201,1199,1197,1198,404],"class_list":["post-632","post","type-post","status-publish","format-standard","hentry","category-php","tag-can-i-use-string-concatenation-to-define-a-class-const-in-php","tag-cant-use-const-and-define-in-php-class","tag-cant-read-file-when-passing-directory-full-path-as-defined-const-in-php","tag-how-does-php-foreach-actually-work","tag-php-class-constant-array","tag-php-const-array","tag-php-const-in-function","tag-php-const-keyword","tag-php-constant-in-class","tag-php-define-scope","tag-reference-what-does-this-symbol-mean-in-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=632"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}