A PHP constant works same as a PHP Variable in terms of storing a value, but the limitation with PHP constants is that the value cannot be changed after getting assigned. In PHP variables, we can manipulate the values by doing different mathematical operations but PHP constants doesn't allow manipulating the value.
In this tutorial we will go through an In-depth analysis og PHP Constants, we will talk about different ways of creating constants, properties of constants. Let's start!
The naming conventions for a constant are similar to that of a variable. A valid constant name can be started with a letter or you can also use an underscore in the starting or anywhere in between the identifier, followed by any letter/numbers/underscore.
A PHP constant can be defined by using
define() function and to retirieve the value of a constant you can simple use the
constant() construct can also be used to read the value of a constant, and it faster than echo() but we will go through this later in the tutorial.
Let's first see how you can create the PHP constants using
define() function uses three arguments:
Constant name, Constant value, Case insensitivity(TRUE/FALSE).
The Case-insensitivity is by default
FALSE in the function, i.e., if you don't specify the insensitivity as TRUE then the Parser will take it as FALSE and the Constant will be case sensititive,
which means that uppercase and lowercase will be treated differently.
Look at the example below to understand how to create a PHP constant by using define() function and then the insensitivity works:
In the example above, we have not specified the insensitivity within the
so the PHP Parser took the default value
Now, the constant is case-sensitive,hence
'GREETING' is treated as a constant but
'greeting' is just a string.
In the example above, we have specified the insensitivity as
'Greeting' will all be treated as a constant and will produce the same result.
constant() is a language construct just like
It is used to read a constant's value when you don't know the name of the constant ,i.e., it is stored in a variable or returned by any function.
In this case you can use the
constant() construct and it also works with class constants.
Look at the example below to understand the working of constant().
In this example above, we used the variable name which holds the name of the constant but still got the constant's value. In this way we can retireve values of constants whose names we don't know or to be more precise we can read the values dynamically during runtime.
In this example we have shown how you can use the constant() construct to access the constant values present inside a class.
PHP Constants are by default Global in nature. In the example below a constant is defined outside a function but still can be accessed from the inside of the function, i.e., the Constant is Global in nature.
|Once the constant is defined, it can never be redefined.||A variable can be undefined as well as redefined easily.|
|A constant can only be defined using define() function. It cannot be defined by any simple assignment.||A variable can be defined by simple assignment (=) operator.|
|There is no need to use the dollar ($) sign before constant during the assignment.||To declare a variable, always use the dollar ($) sign before the variable.|
|Constants do not follow any variable scoping rules, and they can be defined and accessed anywhere.||Variables can be declared anywhere in the program, but they follow variable scoping rules.|
|Constants are the variables whose values can't be changed throughout the program.||The value of the variable can be changed.|
|By default, constants are global.||Variables can be local, global, or static.|