A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. By default, a constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.
To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.
As indicated by the name, this function will return the value of the constant.
This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>
Only scalar data (boolean, integer, float and string) can be contained in constants.
Arvind Dubey is founder of Developer Blog. He is an engineer as per his education and blogger by his profession. Before becoming a professional blogger. he decied to be his own boss and started blogging part time. His passion is blogging and under shoutmeloud, he blogs on topics like blogging tips, core php, mysql, javascript, WordPress, Web tools, SEO and so on
1406, Chember Block, Block D, Sector 14, Rohini, Delhi, 110085
Email: info@developerblog.in