{"id":203048,"date":"2025-07-03T14:46:57","date_gmt":"2025-07-03T14:46:57","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=203048"},"modified":"2025-07-04T12:44:30","modified_gmt":"2025-07-04T12:44:30","slug":"php-constants","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/php\/php-constants\/","title":{"rendered":"PHP Constants"},"content":{"rendered":"\n<p><strong>PHP Constants are the identifiers that cannot be changed. Learn how to create a PHP Constant, and know more about pre-defined constants &amp; Magic constants with examples:<\/strong><\/p>\n\n\n\n<p>In this tutorial, you will learn what a PHP constant is, how to create PHP constants (using the PHP define() function and the const keyword), predefined constants, magic constants, differences between constants and variables, and frequently asked questions (FAQs).<\/p>\n\n\n\n<p>Please note that we have used PHP version 7 in all examples.<\/p>\n\n\n\n<p>Let\u2019s begin!<\/p>\n\n\n\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/php\/\">In-Depth PHP Tutorials for Beginners<\/a><\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">PHP Constants: In-Depth Guide<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2021\/12\/PHP-Constants.png\"><img decoding=\"async\" width=\"700\" height=\"394\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2021\/12\/PHP-Constants.png\" alt=\"PHP Constants\" class=\"wp-image-203087\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2021\/12\/PHP-Constants.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2021\/12\/PHP-Constants-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/figure>\n\n\n\n<p>A PHP constant is a name for a simple value. Unlike variables, we cannot change constants while running the script.<\/p>\n\n\n\n<p><strong>The characteristics of constants are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A constant name starts with a letter or _ (the underscore character).<\/li>\n\n\n\n<li>Unlike variables, no leading $ sign.<\/li>\n\n\n\n<li>By default, constant names are case sensitive (we can make them case insensitive while defining).<\/li>\n\n\n\n<li>The scope is global.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create a PHP Constant<\/h2>\n\n\n\n<p><strong>In PHP, there are two ways to define a constant, as shown in the list below:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using the <strong>define()<\/strong> function<\/li>\n\n\n\n<li>using the <strong>const<\/strong> keyword<\/li>\n<\/ol>\n\n\n\n<p><strong>#1) Using the PHP define() Function<\/strong><\/p>\n\n\n\n<p><strong>The syntax is as follows:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">define(name, value, case-insensitive)<\/pre>\n\n\n\n<p>The above syntax has three parameters: <strong>name<\/strong> (required parameter), <strong>value<\/strong> (required parameter), and <strong>case-insensitivity<\/strong>. The Default value of case-insensitivity is false and is only required if you create a case-insensitive constant name.<\/p>\n\n\n\n<p><strong>A few valid constant names are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>define(&#8220;FOO&#8221;, &#8220;some text&#8221;);<\/li>\n\n\n\n<li>define(&#8220;FOO2&#8221;, &#8220;some text&#8221;);<\/li>\n\n\n\n<li>define(&#8220;FOO_BAR&#8221;, &#8220;some text&#8221;);<\/li>\n<\/ul>\n\n\n\n<p><strong>An invalid constant name includes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>define(&#8220;2FOO&#8221;, &#8220;some text&#8221;);<\/li>\n<\/ul>\n\n\n\n<p>It is invalid, as the name cannot start with a number.<\/p>\n\n\n\n<p>Let\u2019s look at some examples.<\/p>\n\n\n\n<p>You can practice these examples by running the following programming codes.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n  \ndefine(&quot;WELCOME&quot;, &quot;Welcome to My World!&quot;);\n \necho WELCOME;\n \n?&gt;\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Welcome to My World!<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong>&nbsp; An array constant<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;We will cover arrays in a later tutorial.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n  \ndefine(&quot;colors&quot;, &#x5B;&quot;Red&quot;,&quot;Yellow&quot;,&quot;Green&quot;]);\n \necho colors&#x5B;0];\n \n?&gt;\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Red<\/pre>\n\n\n\n<p><strong>#2) Using the const Keyword<\/strong><\/p>\n\n\n\n<p><strong>The syntax is as follows:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const NAME = value;<\/pre>\n\n\n\n<p>In the above syntax, NAME is not case-sensitive but is usually written in uppercase.<\/p>\n\n\n\n<p>Let\u2019s look at some examples.<\/p>\n\n\n\n<p>You can practice these examples by running the following programming code.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n  \nconst MY_NUM = 7;\n \necho MY_NUM;\n \n?&gt;\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">7<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n  \nclass MyPet {\n  const NAME = &quot;Tommy&quot;;\n}\n \necho MyPet::NAME;\n \n?&gt;\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tommy<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Core Predefined Constants<\/h2>\n\n\n\n<p><strong>They are defined by the PHP core. There are 58 predefined constants, as shown in the list below:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>PHP_VERSION<\/li>\n\n\n\n<li>PHP_MAJOR_VERSION<\/li>\n\n\n\n<li>PHP_MINOR_VERSION<\/li>\n\n\n\n<li>PHP_RELEASE_VERSION<\/li>\n\n\n\n<li>PHP_VERSION_ID<\/li>\n\n\n\n<li>PHP_EXTRA_VERSION<\/li>\n\n\n\n<li>PHP_ZTS<\/li>\n\n\n\n<li>PHP_DEBUG<\/li>\n\n\n\n<li>PHP_MAXPATHLEN<\/li>\n\n\n\n<li>PHP_OS<\/li>\n\n\n\n<li>PHP_OS_FAMILY<\/li>\n\n\n\n<li>PHP_SAPI<\/li>\n\n\n\n<li>PHP_EOL<\/li>\n\n\n\n<li>PHP_INT_MAX<\/li>\n\n\n\n<li>PHP_INT_MIN<\/li>\n\n\n\n<li>PHP_INT_SIZE<\/li>\n\n\n\n<li>PHP_FLOAT_DIG<\/li>\n\n\n\n<li>PHP_FLOAT_EPSILON<\/li>\n\n\n\n<li>PHP_FLOAT_MIN<\/li>\n\n\n\n<li>PHP_FLOAT_MAX<\/li>\n\n\n\n<li>DEFAULT_INCLUDE_PATH<\/li>\n\n\n\n<li>PEAR_INSTALL_DIR<\/li>\n\n\n\n<li>PEAR_EXTENSION_DIR<\/li>\n\n\n\n<li>PHP_EXTENSION_DIR<\/li>\n\n\n\n<li>PHP_PREFIX<\/li>\n\n\n\n<li>PHP_BINDIR<\/li>\n\n\n\n<li>PHP_BINARY<\/li>\n\n\n\n<li>PHP_MANDIR<\/li>\n\n\n\n<li>PHP_LIBDIR<\/li>\n\n\n\n<li>PHP_DATADIR<\/li>\n\n\n\n<li>PHP_SYSCONFDIR<\/li>\n\n\n\n<li>PHP_LOCALSTATEDIR<\/li>\n\n\n\n<li>PHP_CONFIG_FILE_PATH<\/li>\n\n\n\n<li>PHP_CONFIG_FILE_SCAN_DIR<\/li>\n\n\n\n<li>PHP_SHLIB_SUFFIX<\/li>\n\n\n\n<li>PHP_FD_SETSIZE<\/li>\n\n\n\n<li>E_ERROR<\/li>\n\n\n\n<li>E_WARNING<\/li>\n\n\n\n<li>E_PARSE<\/li>\n\n\n\n<li>E_NOTICE<\/li>\n\n\n\n<li>E_CORE_ERROR<\/li>\n\n\n\n<li>E_CORE_WARNING<\/li>\n\n\n\n<li>E_COMPILE_ERROR<\/li>\n\n\n\n<li>E_COMPILE_WARNING<\/li>\n\n\n\n<li>E_USER_ERROR<\/li>\n\n\n\n<li>E_USER_WARNING<\/li>\n\n\n\n<li>E_USER_NOTICE<\/li>\n\n\n\n<li>E_RECOVERABLE_ERROR<\/li>\n\n\n\n<li>E_DEPRECATED<\/li>\n\n\n\n<li>E_USER_DEPRECATED<\/li>\n\n\n\n<li>E_ALL<\/li>\n\n\n\n<li>E_STRICT<\/li>\n\n\n\n<li>__COMPILER_HALT_OFFSET__<\/li>\n\n\n\n<li>true<\/li>\n\n\n\n<li>false<\/li>\n\n\n\n<li>null<\/li>\n\n\n\n<li>PHP_WINDOWS_EVENT_CTRL_C<\/li>\n\n\n\n<li>PHP_WINDOWS_EVENT_CTRL_BREAK<\/li>\n<\/ol>\n\n\n\n<p><strong>The list below shows more details about selected predefined constants. <\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP_VERSION\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current version of PHP within the format of major.minor.release[extra].<\/li>\n\n\n\n<li>Output data type &#8211; string<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_MAJOR_VERSION\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current <strong>major<\/strong> version of PHP.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n\n\n\n<li>Ex: 7 from version 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_MINOR_VERSION\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current <strong>minor<\/strong> version of PHP.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n\n\n\n<li><strong>Ex<\/strong>: 2 from version 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_RELEASE_VERSION\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current <strong>release<\/strong> version of PHP.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n\n\n\n<li><strong>Ex<\/strong>: 0 from version 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_VERSION_ID\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current version of PHP. It is useful when comparing versions.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n\n\n\n<li><strong>Ex<\/strong>: 70200 from version 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_EXTRA_VERSION\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The current <strong>extra<\/strong> version of PHP. Useful to specify a package version.<\/li>\n\n\n\n<li>Output data type &#8211; string<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_DEBUG\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Assist in debugging PHP code.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_MAXPATHLEN\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The maximum length of file names (including path) is supported by this PHP build.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_OS\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The operating system (OS) PHP was built for.<\/li>\n\n\n\n<li>Output data type &#8211; string<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_OS_FAMILY\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The OS family PHP was built for.<\/li>\n\n\n\n<li>Output data type &#8211; string<\/li>\n\n\n\n<li>Available since PHP 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_INT_MAX\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The largest integer supported during this PHP build.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_INT_MIN\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The smallest integer supported during this PHP build.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_INT_SIZE\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The size of an integer in bytes during this PHP build.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_FLOAT_MIN\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Smallest representable positive floating-point number.<\/li>\n\n\n\n<li>Output data type &#8211; float<\/li>\n\n\n\n<li>Available since PHP 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>PHP_FLOAT_MAX\n<ul class=\"wp-block-list\">\n<li>Description &#8211; The largest representable floating-point number.<\/li>\n\n\n\n<li>Output data type &#8211; float<\/li>\n\n\n\n<li>Available since PHP 7.2.0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>E_ERROR\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Fatal run-time errors.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>E_WARNING\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Run-time warnings (non-fatal errors)<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>E_PARSE\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Compile-time parse errors.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>E_NOTICE\n<ul class=\"wp-block-list\">\n<li>Description &#8211; Run-time notices.<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>E_ALL\n<ul class=\"wp-block-list\">\n<li>Description &#8211; All errors and warnings (as supported).<\/li>\n\n\n\n<li>Output data type &#8211; int<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Magic Constants<\/h2>\n\n\n\n<p>PHP provides a special set of predefined constants called magic constants. They can change depending on the place where they are used. They start with two underscores and also end with two underscores. Furthermore, magic constants are case-insensitive.<\/p>\n\n\n\n<p><strong>The following table shows the magic constants in PHP:<\/strong><\/p>\n\n\n\n<div id=\"tablepress-3553-scroll-wrapper\" class=\"tablepress-scroll-wrapper\">\n<table id=\"tablepress-3553\" class=\"tablepress tablepress-id-3553 tablepress-responsive\">\n<thead>\n<tr class=\"row-1\">\n\t<td class=\"column-1\"><\/td><th class=\"column-2\">Name<\/th><th class=\"column-3\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">1<\/td><td class=\"column-2\">__LINE__<\/td><td class=\"column-3\">The line number of the current file.<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">2<\/td><td class=\"column-2\">__FILE__<\/td><td class=\"column-3\">The full path of the file and the file name with symbolic links resolved.<br \/>\nIf it uses inside an include, the included file name will return.<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">3<\/td><td class=\"column-2\">__DIR__<\/td><td class=\"column-3\">The directory of the file.<br \/>\nIf it uses inside an include, the included file directory will return. It is equivalent to dirname(__FILE__). If it is not the root directory, it will not contain a trailing slash.<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">4<\/td><td class=\"column-2\">__FUNCTION__<\/td><td class=\"column-3\">The name of the function.<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">5<\/td><td class=\"column-2\">__CLASS__<\/td><td class=\"column-3\">The name of the class. It includes the namespace it was declared in (Ex: Foo\\Bar).<br \/>\nIf it is used in a trait method, __CLASS__ is the class name the trait is used in.<\/td>\n<\/tr>\n<tr class=\"row-7\">\n\t<td class=\"column-1\">6<\/td><td class=\"column-2\">__TRAIT__<\/td><td class=\"column-3\">The name of the trait. It includes the namespace it was declared in (Ex: Foo\\Bar).<\/td>\n<\/tr>\n<tr class=\"row-8\">\n\t<td class=\"column-1\">7<\/td><td class=\"column-2\">__METHOD__<\/td><td class=\"column-3\">The name of the class method.<\/td>\n<\/tr>\n<tr class=\"row-9\">\n\t<td class=\"column-1\">8<\/td><td class=\"column-2\">__NAMESPACE__<\/td><td class=\"column-3\">The name of the current namespace.<\/td>\n<\/tr>\n<tr class=\"row-10\">\n\t<td class=\"column-1\">9<\/td><td class=\"column-2\">ClassName::class<\/td><td class=\"column-3\">The name of the fully qualified class.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<!-- #tablepress-3553 from cache -->\n\n\n<h2 class=\"wp-block-heading\">Differences: Constants Vs Variables<\/h2>\n\n\n\n<p>Constants and variables have similarities, but there are some significant differences, too.<\/p>\n\n\n\n<div id=\"tablepress-3554-scroll-wrapper\" class=\"tablepress-scroll-wrapper\">\n<table id=\"tablepress-3554\" class=\"tablepress tablepress-id-3554 tablepress-responsive\">\n<thead>\n<tr class=\"row-1\">\n\t<td class=\"column-1\"><\/td><th class=\"column-2\">Constants<\/th><th class=\"column-3\">Variables<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">1<\/td><td class=\"column-2\">A name or an identifier for a value.<\/td><td class=\"column-3\">A name or symbol to store a value.<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">2<\/td><td class=\"column-2\">Syntax: define(name, value, case-insensitive)<\/td><td class=\"column-3\">Syntax: Leading $ sign followed by the name of the variable (example: $name)<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">3<\/td><td class=\"column-2\">The value cannot be changed once assigned.<\/td><td class=\"column-3\">Can be reassigned a value.<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">4<\/td><td class=\"column-2\">No leading $ sign is required.<\/td><td class=\"column-3\">Must have a leading $ sign to be executed.<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">5<\/td><td class=\"column-2\">By default, constants are global (universally global).<\/td><td class=\"column-3\">Variables can be local or global.<\/td>\n<\/tr>\n<tr class=\"row-7\">\n\t<td class=\"column-1\">6<\/td><td class=\"column-2\">Useful for storing information that does not change.<\/td><td class=\"column-3\">Useful for storing information that might change.<\/td>\n<\/tr>\n<tr class=\"row-8\">\n\t<td class=\"column-1\">7<\/td><td class=\"column-2\">Example:<\/td><td class=\"column-3\">Example:<\/td>\n<\/tr>\n<tr class=\"row-9\">\n\t<td class=\"column-1\"><\/td><td class=\"column-2\">define(&#8216;TEST&#8217;, &#8216;Software Testing Help&#8217;)<\/td><td class=\"column-3\">$test = &#8216;Software Testing Help&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<!-- #tablepress-3554 from cache -->\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1751041274793\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>1. What are PHP constants?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Constants are names used for simple values. Furthermore, they cannot be changed while running the script.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751041304497\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>2. How is a PHP constant created?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>There are two methods to create a constant. They are using the PHP <strong>define()<\/strong> function and using the <strong>const<\/strong> keyword.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751041412146\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>3. Give an example of a PHP constant.<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Please refer to the example below:<\/p>\n<p>const TEST = &#8216;Software Testing Help&#8217;;<\/p>\n<p>(OR)<\/p>\n<p>define(&#8216;TEST&#8217;, &#8216;Software Testing Help&#8217;)<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751041485761\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>4. Can we redefine a constant in PHP?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>No, unlike variables, we cannot redefine a constant once it is defined.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751041540074\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>5. Show in an example how a function can access a constant that is defined outside the function.<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>A function can easily access a constant as constants have a global scope. Please refer to the example below:<\/p>\n<p>&lt;?php<br \/>define(&#8220;GREETING&#8221;, &#8220;Have a nice day!&#8221;);<\/p>\n<p>function myExample() {<br \/>    echo GREETING;<br \/>}<\/p>\n<p>myExample();<br \/>?&gt;<\/p>\n<p>It will echo the following output:<\/p>\n<p>Have a nice day!<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751041889709\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>6. What is the main difference between const and define in PHP?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>The main difference is that <strong>const<\/strong> defines a constant at compile-time, whereas <strong>define()<\/strong> defines a constant at run-time.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>A PHP constant is a name for a simple value and cannot be changed later on. The PHP define() function or the const keyword is used to create a constant.<\/p>\n\n\n\n<p>Unlike variables, constants are accessible from anywhere in the script. Furthermore, there are predefined constants as well as magic constants in PHP.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-displaying-variables\/\">PREV Tutorial<\/a> | <a href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-functions\/\">NEXT Tutorial<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"203048\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>PHP Constants are the identifiers that cannot be changed. Learn how to create a PHP Constant, and know more about pre-defined constants &amp; Magic constants with examples: In this tutorial, you will learn what a PHP constant is, how to create PHP constants (using the PHP define() function and the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"PHP Constants\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-constants\/#more-203048\" aria-label=\"Read more about PHP Constants\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":203087,"parent":392052,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[412],"tags":[],"class_list":{"0":"post-203048","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-php"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/203048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=203048"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/203048\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/392052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/203087"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=203048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=203048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=203048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}