{"id":171,"date":"2016-07-20T10:28:41","date_gmt":"2016-07-20T10:28:41","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/20\/define-constant-php\/"},"modified":"2024-03-03T15:04:53","modified_gmt":"2024-03-03T15:04:53","slug":"define-constant-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/define-constant-php\/","title":{"rendered":"Define Constant PHP"},"content":{"rendered":"<h2>Constant in PHP ?<\/h2>\n<p>Constant are identifiers for a memory location Which Can be change according to requirement of program .In <a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP <\/a> Constant does NOT support <a href=\"https:\/\/barkhane.com\/php\/interpolation\/\" target=\"_blank\" rel=\"noopener\">interpolation <\/a>. In <a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP <\/a>Constant must be in Upper Case eg. <strong>CONST \u00a0.<\/strong>To define Constant we have to use function define();<\/p>\n<blockquote><p>Syntax :<br \/>\ndefine(\\&#8217;name of constant \\&#8217;,\\&#8217; value of constant\\&#8217;)<br \/>\neg: define(\\&#8217;PIE\\&#8217;,\\&#8217;3.14\\&#8217;);<br \/>\necho PIE;\/\/\/ will result to 3.14<\/p><\/blockquote>\n<p>NOTE:Unlike variable \u00a0If we Try to define same Constant again then it throw a notice Already define.<\/p>\n<p>&nbsp;<\/p>\n<p>Another way of Defining Constant is key word <strong>const <\/strong>.<\/p>\n<p>eg:<span class=\"comment\">\u00a0 <\/span><span class=\"keyword\">const <\/span><span class=\"default\">MIN_VALUE <\/span><span class=\"keyword\">= <\/span><span class=\"default\">0.0;<\/span><\/p>\n<p>echo\u00a0<span class=\"comment\">\u00a0 <\/span><span class=\"default\">MIN_VALUE ;\u00a0\/\/\/ will result to\u00a00<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The fundamental difference between those two ways is that <strong>const<\/strong> defines constants at <strong>compile time<\/strong>, whereas <strong>define<\/strong> defines them at<strong> run time<\/strong>.<\/p>\n<p>Note : <strong>const cannot be used to conditionally define constants. It has to be used in the outermost scope<\/strong><\/p>\n<p>eg:<\/p>\n<p>&lt;?php<\/p>\n<p>$x=10 and $y=10;<br \/>\nif ($x==$y) {<br \/>\nconst CONST_VAL1 = \\&#8217;CONST\\&#8217;; \/\/ invalid produce error<br \/>\n}<br \/>\n\/\/ but<br \/>\nif ($x==$y) {<br \/>\ndefine(\\&#8217;CONST_VAL2\\&#8217;, \\&#8217;CONST\\&#8217;); \/\/ valid<br \/>\n}<br \/>\necho CONST_VAL1;\u00a0\/\/ produce error<br \/>\necho CONST_VAL2;<\/p>\n<p>?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constant in PHP ? Constant are identifiers for a memory location Which Can be change according to requirement of program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}