PHP has a unique ways of doing things and carrying out operations which is quite different from other scripting languages like JavaScript and Python. This shot shows you how PHP works with constants.
A constant in PHP:
$
special character; they can only start with an underscore or letters.define()
functionTo create constants you can use the define()
function.
define(name,Value,case-insentive)
This function takes the following parameters:
name
: Name of the constant to be made.Value
: The value to be assigned to the constant.case-insensitive
: Whether the the name should be case-insensitive or not. Defaults to false
if not indicated.const
keywordThis is a language construct rather than a function. It is meant to define a constant at the point of the compilation of code.
Constants declared with the const
keyword have to be declared at points which they can be accessible to the piece of code which will need them. This simply means they are not like those declared with the define()
function, which remain global no matter where they are declared.
The keyword const
constants have to be declared in the global scope. Constants declared with this keyword are case-sensitive.
const example_const = 56;
With this declaration, example_const
is now a constant with the value 56 that cannot change.
constant()
functionFor constants declared with the define()
function and the const
keyword, their values can be returned using the
constant()
function passing the name of the constant in quotes as an argument. Any constant can be used by just their names.
See the code below to understand better.
<?phpdefine("defined", "just a defined constant",true);//true makes it not case sensitiveecho DEFINED ."<br>";echo constant("defined")."<br>";//using constconst tryconst = "A lot has happened";echo tryconst."<br>";echo constant("tryconst");?>
RELATED TAGS
CONTRIBUTOR