{"id":57,"date":"2021-03-08T00:05:11","date_gmt":"2021-03-08T00:05:11","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=57"},"modified":"2025-04-05T14:07:42","modified_gmt":"2025-04-05T14:07:42","slug":"php-constants","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-constants\/","title":{"rendered":"PHP Constants"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you learn about PHP constants and how to use the <code>define()<\/code> function and <code>const<\/code> keyword to define constants.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-constants'>Introduction to PHP constants <a href=\"#introduction-to-php-constants\" class=\"anchor\" id=\"introduction-to-php-constants\" title=\"Anchor for Introduction to PHP constants\">#<\/a><\/h2>\n\n\n\n<p>A constant is simply a name that holds a single value. As its name implies, the value of a constant cannot be changed during the execution of the PHP script.<\/p>\n\n\n\n<p>To define a constant, you use the &nbsp;<code>define()<\/code>&nbsp;function. The &nbsp;<code>define()<\/code> function takes the constant&#8217;s name as the first argument and the constant value as the second. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\ndefine(<span class=\"hljs-string\">'WIDTH'<\/span>,<span class=\"hljs-string\">'1140px'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> WIDTH;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAgCgpkZWZpbmUoJ1dJRFRIJywnMTE0MHB4Jyk7CmVjaG8gV0lEVEg7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>By convention, constant names are uppercase. Unlike a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a>, the constant name doesn&#8217;t start with the dollar sign(<code>$<\/code>).<\/p>\n\n\n\n<p>By default, constant names are case-sensitive. It means that <code>WIDTH<\/code> and <code>width<\/code> are different constants.<\/p>\n\n\n\n<p class=\"note\">It&#8217;s possible to define case-insensitive constants. However, it&#8217;s been deprecated since PHP 7.3<\/p>\n\n\n\n<p>In PHP 5, a constant can hold a simple value like a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-int\/\">number<\/a>, a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-string\/\">string<\/a>, or a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-boolean\/\">boolean <\/a>value. From PHP 7.0, a constant can hold an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\ndefine( <span class=\"hljs-string\">'ORIGIN'<\/span>, &#91;<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>] );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmRlZmluZSggJ09SSUdJTicsIFswLCAwXSApOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Like superglobal variables, you can access constants from anywhere in the script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-const-keyword'>The const keyword <a href=\"#the-const-keyword\" class=\"anchor\" id=\"the-const-keyword\" title=\"Anchor for The const keyword\">#<\/a><\/h2>\n\n\n\n<p>PHP provides you with another way to define a constant via the <code>const<\/code> keyword. Here&#8217;s the syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">const<\/span> CONSTANT_NAME = value;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you define the constant name after the <code>const<\/code> keyword. You use the assignment operator (=) and the constant value to assign a value to a constant. The constant value can be scalar, e.g., a number, a string, or an array.<\/p>\n\n\n\n<p>The following example uses the <code>const<\/code> keyword to define the <code>SALES_TAX<\/code> constant:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> SALES_TAX = <span class=\"hljs-number\">0.085<\/span>;\n\n$gross_price = <span class=\"hljs-number\">100<\/span>;\n\n$net_price = $gross_price * (<span class=\"hljs-number\">1<\/span> + SALES_TAX);\n\n<span class=\"hljs-keyword\">echo<\/span> $net_price; <span class=\"hljs-comment\">\/\/ 108.5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNvbnN0IFNBTEVTX1RBWCA9IDAuMDg1OwoKJGdyb3NzX3ByaWNlID0gMTAwOwoKJG5ldF9wcmljZSA9ICRncm9zc19wcmljZSAqICgxICsgU0FMRVNfVEFYKTsKCmVjaG8gJG5ldF9wcmljZTsgLy8gMTA4LjU\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The following example uses the <code>const<\/code> keyword to define the RGB constant that holds an array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> RGB = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='define-vs-const'>define vs. const <a href=\"#define-vs-const\" class=\"anchor\" id=\"define-vs-const\" title=\"Anchor for define vs. const\">#<\/a><\/h2>\n\n\n\n<p>First, the <code>define()<\/code> is a function while the <code>const<\/code> is a language construct.<\/p>\n\n\n\n<p>It means that the <code>define()<\/code> function defines a constant at run-time, whereas the <code>const<\/code> keyword defines a constant at compile time.<\/p>\n\n\n\n<p>In other words, you can use the <code>define()<\/code> function to define a constant conditionally like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">if<\/span>(condition) \n{\n    define(<span class=\"hljs-string\">'WIDTH'<\/span>, <span class=\"hljs-string\">'1140px'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, you cannot use the <code>const<\/code> keyword to define a constant this way. For example, the syntax of the following code is invalid:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-keyword\">if<\/span>(condition) \n{\n   <span class=\"hljs-keyword\">const<\/span> WIDTH = <span class=\"hljs-string\">'1140px'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, the <code>define()<\/code> function allows you to define a constant with the name that comes from an expression. For example, the following defines three constants <code>OPTION_1<\/code>, <code>OPTION_2<\/code>, and <code>OPTION_3<\/code> with the values 1, 2, and 3.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\ndefine(<span class=\"hljs-string\">'PREFIX'<\/span>, <span class=\"hljs-string\">'OPTION'<\/span>);\n\n\ndefine(PREFIX . <span class=\"hljs-string\">'_1'<\/span>, <span class=\"hljs-number\">1<\/span>);\ndefine(PREFIX . <span class=\"hljs-string\">'_2'<\/span>, <span class=\"hljs-number\">2<\/span>);\ndefine(PREFIX . <span class=\"hljs-string\">'_3'<\/span>, <span class=\"hljs-number\">3<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, you cannot use the <code>const<\/code> keyword to define a constant name derived from an expression.<\/p>\n\n\n\n<p>Unless you want to define a constant conditionally or use an expression, you can use the <code>const<\/code> keyword to define constants to clarify the code.<\/p>\n\n\n\n<p class=\"note\">Note that you can use the <code>const<\/code> keyword to define constants inside <a href=\"https:\/\/phptutorial.net\/php-oop\/php-class-constants\/\" target=\"_blank\" rel=\"noreferrer noopener\">classes<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A constant is a name that holds a simple value that cannot be changed during the execution of the script. From PHP 7, a constant can hold an array.<\/li>\n\n\n\n<li>A constant can be accessed from anywhere in the script.<\/li>\n\n\n\n<li>Use the <code>define()<\/code> function or <code>const<\/code> keyword to define a constant.<\/li>\n\n\n\n<li>Use the <code>define()<\/code> function if you want to define a constant conditionally or using an expression.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"57\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Constants\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"57\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Constants\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you learn how to use PHP constants in your script.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-57","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/57\/revisions"}],"predecessor-version":[{"id":2944,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/57\/revisions\/2944"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}