<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":4564,"date":"2022-08-03T12:03:36","date_gmt":"2022-08-03T12:03:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4564"},"modified":"2022-08-03T12:03:36","modified_gmt":"2022-08-03T12:03:36","slug":"what-are-the-ways-to-define-a-constant-in-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/","title":{"rendered":"What are the ways to define a constant in PHP ?"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/www.wikitechy.com\/php\/php-constants\">PHP constants<\/a> are identifier or name that can&#8217;t be changed during the execution of the script except for magic constants, which are not really constants.<\/li>\n<li>Constants can never be undefined or changed, they are similar to the variable except once defined.<\/li>\n<li>PHP constants follow the same PHP variable rules and it remains constant across the entire program.<\/li>\n<li>Conventionally, in uppercase letters PHP constants should be defined and it can be started with an underscore or letter only.<\/li>\n<li>PHP constants consists of two types, they are:\n<ul>\n<li>Using define () function<\/li>\n<li>Using const keyword<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"using-define-function\">Using define () function<\/h2>\n<ul>\n<li>In PHP use the define () function to create a constant and it defines constant at run time.<\/li>\n<li>In this function name specifies the constant name, value specifies the constant value and case-insensitive specifies whether a constant is case-insensitive.<\/li>\n<li>By default, if value is false it means it is case sensitive.<\/li>\n<\/ul>\n<h3 id=\"sample-code\">Sample Code<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/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;MESSAGE&quot;,&quot;Welcome to Wikitechy&quot;,true);\/\/not case sensitive    <br\/>echo MESSAGE, &quot;&lt;\/br&gt;&quot;;    <br\/>echo message;    <br\/>?&gt;    <\/code><\/pre> <\/div>\n<h3 id=\"output\">Output<\/h3>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4565\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png\" alt=\"\" width=\"1229\" height=\"309\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png 1229w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-300x75.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-1024x257.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-768x193.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-390x98.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-820x206.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function-1180x297.png 1180w\" sizes=\"(max-width: 1229px) 100vw, 1229px\" \/><\/p>\n<h2 id=\"using-const-keyword\">Using const Keyword<\/h2>\n<ul>\n<li>PHP introduces a const keyword to create a constant and it defines constants at compile time.<\/li>\n<li>It constructs a language, not a function.<\/li>\n<li>The constant defined using const keyword are\u00a0<strong>case-sensitive<\/strong>.<\/li>\n<\/ul>\n<h3 id=\"sample-code-2\">Sample Code<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/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;MSG&quot;, &quot;Wikitechy&quot;);  <br\/>    echo MSG, &quot;&lt;\/br&gt;&quot;;  <br\/>    echo constant(&quot;MSG&quot;);  <br\/>    \/\/both are similar  <br\/>?&gt;  <\/code><\/pre> <\/div>\n<h3 id=\"output-2\">Output<\/h3>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-4566 aligncenter\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword.png\" alt=\"\" width=\"1230\" height=\"290\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword.png 1230w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-300x71.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-1024x241.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-768x181.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-390x92.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-820x193.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/const-keyword-1180x278.png 1180w\" sizes=\"(max-width: 1230px) 100vw, 1230px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP constants are identifier or name that can&#8217;t be changed during the execution of the script except for magic constants, which are not really constants. Constants can never be undefined or changed, they are similar to the variable except once defined. PHP constants follow the same PHP variable rules and it remains constant across the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[8182],"tags":[17739,17740,17664,17745,17737,17742,17741,17738,17743,17744],"class_list":["post-4564","post","type-post","status-publish","format-standard","hentry","category-php","tag-how-to-define-constants-in-php","tag-how-to-use-constants-in-php","tag-magic-constants-in-php","tag-php-constant-case-insensitive","tag-php-constants","tag-php-constants-array","tag-php-constants-in-class","tag-php-constants-types","tag-php-protected-static-const","tag-php-unset-constant"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What are the ways to define a constant in PHP ? - PHP Constants<\/title>\n<meta name=\"description\" content=\"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can&#039;t be changed during the execution of the script\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are the ways to define a constant in PHP ? - PHP Constants\" \/>\n<meta property=\"og:description\" content=\"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can&#039;t be changed during the execution of the script\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-03T12:03:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/\",\"name\":\"What are the ways to define a constant in PHP ? - PHP Constants\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png\",\"datePublished\":\"2022-08-03T12:03:36+00:00\",\"dateModified\":\"2022-08-03T12:03:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can't be changed during the execution of the script\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png\",\"width\":1229,\"height\":309},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What are the ways to define a constant in PHP ? - PHP Constants","description":"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can't be changed during the execution of the script","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/","og_locale":"en_US","og_type":"article","og_title":"What are the ways to define a constant in PHP ? - PHP Constants","og_description":"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can't be changed during the execution of the script","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/","og_site_name":"Wikitechy","article_published_time":"2022-08-03T12:03:36+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png"}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/","name":"What are the ways to define a constant in PHP ? - PHP Constants","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png","datePublished":"2022-08-03T12:03:36+00:00","dateModified":"2022-08-03T12:03:36+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"What are the ways to define a constant in PHP ? - PHP constants are identifier or name that can't be changed during the execution of the script","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-the-ways-to-define-a-constant-in-php\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/define-function.png","width":1229,"height":309},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4564"}],"version-history":[{"count":1,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4564\/revisions"}],"predecessor-version":[{"id":4567,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4564\/revisions\/4567"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}