PHP Constant

The constants are identifiers in PHP used to define global data. If a script requires a global value that should be accessed from any scope, a constant can be used to define these. It is created using the define() function and can be assigned any fixed value. Once the "constant" is defined, the value cannot be changed during the script execution. Hence the value remains unchanged during the lifespan of the script execution.

It is defined using a letter, underscore, and numbers similar to the variable name must start with a letter or underscore. Any special character must not be used to define a constant. It is always case-sensitive.

PHP define() function

A constant is created using the define() function, which takes two arguments the name of the constant and its value. The constant can be used by referring to its name. Consider the following example.

			

<?php
define("DOMAIN", "https://www.tutorialsnation.com/");
echo "<title> PHP Tutorials - " . DOMAIN  . "</title>";
?>

				

Following standard convention, constants are written in upper-case to be identified in the script easily. The following example creates a constant which is not case-sensitive. The last parameter is assigned as TRUE to achieve it.

			

<?php
define("DOMAIN","www.tutorialsnation.com",TRUE);
echo "<title> PHP Tutorials - " . DOMAIN  . "</title>";
echo "<title> PHP Tutorials - " . domain  . "</title>";
?>

				

Output:

// Output of DOMAIN PHP Tutorials - www.tutorialsnation.com // Output of domain PHP Tutorials - https://www.tutorialsnation.com/
			

<?php
define("DOMAIN","www.tutorialsnation.com",FALSE);
echo "<title> PHP Tutorials - " . DOMAIN  . "</title>";
echo "<title> PHP Tutorials - " . domain  . "</title>";
?>

				

Output:

// Output of DOMAIN PHP Tutorials - www.tutorialsnation.com // Output of domain Notice: Use of undefined constant domain - assumed 'domain' in C:\xampp\htdocs\tutorialsnationconstant.php on line 4 PHP Tutorials - www.tutorialsnation.com

The scope of the constant is global, whether you define the variable inside a function or in the global script.

			

<?php
// Constant define in global scope
define('ACONST', 'hello');

function firstfunc() {
// Constant define inside the function
define('BCONST', 'world');
}
firstfunc();
echo ACONST;
echo "<br />";
echo BCONST;

				

Output:

//value of constant defined in the global scope hello //value of constant defined in the global scope world

In the above example, We defined a constant ACONST in the global scope and BCONST from inside the function, and you see both are available in the global scope.

PHP const Keyword

Like global constants, assigned through the define() function, PHP provides another way to define a constant using the const keyword.

			

const ACONST = 1; 
echo ACONST; 

				

Output:

1

To declare a constant in a class, you need to do is precede an identifier with the const keyword. You can not use the define() function inside a class. The keywords public, private, protected, or static can be used while declaring a class const as it is always public and static. Using self-notation, constants can be accessed directly through the class or within an object.

			

class myTest
{ 
const ACONST = 1; 
} 
echo myTest:: ACONST; 

				

Output:

1