PHP Constant Types

PHP constants are basically simple functions or names that can store a static value. Programs can execute the fixed value stored in a constant. Constants are static, not dynamic. Means, when a constant is defined it cannot be undefined again in the same script.

A constant is most likely variables, it stores value like a variable and it also can execute the stored value just like a variable. There is the only difference between constant and variable, a stored value in a constant never be changed while execution. Unlike variables, a constant never be undefined or redefined once the constant is already defined.

In PHP programming convention, constants are always defined in uppercase format by default. But if you want to define in lowercase format, then you have to mention it while defining a constant. Like PHP variables, constants also have the naming convention, you must start by a letter or underscores followed by the name while defining a constant.

Constant Syntax

In the previous chapter, we have learned how to define a variable. Unlike PHP variables, you don’t need to use a dollar sign while defining a constant. PHP constant syntax is completely different from variable syntax. Constants use a simple function to be defined.

Thedefine() function is used to define a constant as shown below:

define("name of the constant"[in upper-case], "value of the constant");

As we already know, PHP constants name always have to be in upper-case letters. But if you want to declare a constant name in the lowercase letter, then you have to mention it as shown below:

define("name of the constant"[in lower-case], "value of the constant", true[that indicates to use lower-case]);


define ("HELLO", "Welcome to PrograCoding");    //You must have to use upper-case letters
echo HELLO."\n";
define ("hello", "Welcome to PrograCoding", true);    //You may use lower-case letters
echo HELLO;


Welcome to PrograCoding
Welcome to PrograCoding

Printing using constant() function

You can also use constant() function in order to execute a constant as shown below:


define("MOTTO", "Hello, world!");
//echoing using constant() function
echo constant("MOTTO")


Hello, world!

Constants are Global Scoped

All the constants defined in a single PHP script will be automatically global. Unlike a PHP variable, constants are always accessible in any location inside or outside of any function.


define("VAR1", "Simple Interpretation of Learning for Self-learners");
//define a function
function testGlobal(){
    echo VAR1;


Simple Interpretation of Learning for Self-learners

« Previous Next »