PHP constants are similar to constant variables in other programming languages. A PHP constant holds a fixed value, which cannot be updated after declaration.
Index
PHP Constants
- PHP Constants are similar to the variables, and they cannot be changed or redefined once they have been created and assigned values.
- They remain constant across the entire program.
- It is useful for storing data that don’t change during the execution of the PHP program.
- Examples of such data are configuration settings such as database username and password, website base URL, etc.
- We can define constants using PHP’s define () function, which contains two arguments, the name of the constant and its value.
- Once we determine, the constant value can be retrieved at any time by referring to its name.
- Unlike variables, Constants are global in nature and hence, can be called anywhere in the script.
Read Also: PHP Data Types
Naming Conventions for PHP Constants
- The naming convention of constants follows the same set of rules as the names of variables.
- A valid constant name must start with a letter or underscore.
- $ Prefix is not necessary for the constant name.
- The PHP constants can be both case-sensitive and insensitive. It depends upon the method using which it has been defined.
PHP Constant Declaration Methods
We can define a constant in PHP using two methods. However, both methods work the same. So, there will be no difference in output or usage.
- Using define() function
- Using const keyword
PHP Constant Declaration Using define() Function
To create a PHP constant, we can use the define() function. For this, we have to declare its name and value, which we won’t be able to change later. It must begin with a letter or underscore (_) and contain as many letters or numbers.
Syntax
define(name, value, case-insensitive);
- Name: It specifies the constant name.
- Value: It specifies the constant value.
- Case Insensitive: It is a boolean value (TRUE/FALSE), which specifies whether a constant is case insensitive or not.
Example 1
<?php define("MSG", "Hello Suman"); echo MSG; ?>
Output
Hello Suman
Example 2
Constant with case-insensitive name
<?php define("MSG","Hello Suman",true);//not case sensitive echo MSG."</br>"; echo message; ?>
Output
Hello Suman Hello Suman
PHP Constant Declaration Using the Const Keyword
The const keyword defines a constant at compile time. The const keyword is not a function; it is a language construct. Constants defined using the const keyword are case sensitive.
Example
<?php const MSG="Hello Suman"; echo MSG; ?>
Output
Hello Suman
Global Constants
In PHP, constants are global and used across the script.
Example
<?php define("HELLO", "Welcome Suman"); function myTest() { echo HELLO; } myTest(); ?>
Output
Welcome Suman
Constant() Function
It is another way to print the value of constants using the constant() function instead of using the echo statement. The constant () method returns the constant value and NULL value if the constant is not defined.
Syntax
constant(name);
Example
<?php define("MSG", "Suman"); echo MSG."</br>"; echo constant("MSG"); //both are similar ?>
Output
Suman Suman
Difference Between Constant and Variable
- In variable, the dollar sign ($) before the variable is mandatory, whereas there is no need to write a dollar sign in constant.
- Once the constant is defined, it can’t be redefined, whereas a variable is flexible.
- A constant can only be defined using a define() function or using the const keyword, whereas a variable set using a simple assignment (=) operator.
- Constants are global by default, whereas variables can be Local, Global, or Static.
- The constants do not follow any variable scoping rules, and they can be defined & accessed anywhere. In contrast, the variable can be declared anywhere, but they follow the variable scoping rules.
Read More: PHP Operators
Conclusion
Constants are useful for storing data that don’t need to change while the PHP program is getting executed. PHP constants are similar to constant variables in other programming languages. A PHP constant holds a fixed value, which cannot be updated after declaration.
We hope you have had a complete understanding of PHP Constants. Do refer to further blogs for the complete modules on other aspects of PHP programming.