Constants in PHP (Definition With Examples)

Like other programming languages, PHP has its own set of programming keywords and built-in functions that are very useful for web and system development.

Additionally, it also gives alternative actions when dealing with constants in the program.

What are constants in PHP?

The word “constant“(in English word) refers to an occurrence that occurs continuously and does not change.

PHP constants, however, are values that should not be modified by the program during regular execution (meaning a value is constant) except for magic constants.

Moreover, a constant is a PHP identifier() that applies to a simple value and its name starts with a letter or underscore.

When a constant has a name, it is said to be “named,” but the terms “constant” and “named constant” are often used interchangeably. 

Create Constants in PHP

In this section, we will try to create constants in PHP which will let you understand its implementation.

Understanding this identifier() will also help you in deepening your knowledge of the PHP programming language.

To start with, you must be familiar with its syntax and parameters to make yourself aware of its proper application.

Constants – Syntax

The syntax below is the proper declaration of constants in a PHP program.

define(name, value, case-insensitive)

A simple reminder “Please make sure to follow the proper syntax delegation of constants in your projects”.

This is to help you with meeting the desired function of your project as well as to avoid unwanted errors or bugs.

Constant – Parameters

The following are the parameters that the constants take when implementing their function.

  • name – serves as the name of a constant
  • value – serves as the value of a constant
  • case-sensitive – is the basis of whether the name of the constant should be case-sensitive (false) or case-insensitive (true). This parameter has ‘false’ as its default value.

Remember that you can apply case-insensitive in a program by changing the default to true.

Moreover, the name and value parameters were required at all times.

The case-sensitive parameter will have its default value as false even if it is not mentioned within the function.

Program Example in Constant with Case-sensitive

In this first example, the program will show how to apply the constant with the case-sensitive declaration. Using the syntax shown above, here’s the first program example.


define("ABC", "Welcome to!");
echo ABC;


Program Example in Constant with Case-sensitive

The program sets the define() function to define the variable "ABC" and assigns "Welcome to!" as its value.

This also uses the ‘echo ABC‘ to display the constant’s value as seen in the output.

The keyword echo is the function that enables the program to display the value of the variable ABC. As a result, the output displays the value ‘Welcome to!

Program Example in Constant with Case-insensitive

The next example shows the application of PHP constants with case-insensitive variables.

To enable the constant, the false default value should be replaced with ‘true‘. Take a look at the program example below.


define("ABC", "Welcome to!", true);
echo abc;


Program Example in Constant with Case-insensitive

As seen in the second example, it displays the same output as the first example. This is made possible by changing the default value of the constant from false to true.

It is to enable the program to support the case-insensitive declaration of the ABC variable in ‘echo abc‘.

PHP – Constant() function

Another way of returning the value of PHP constants is through the use of the constant() function. This function returns the value of a constant without the use of an echo or defined function.

To apply the constant() function in our program, we must use its proper declaration or syntax.




    define("ABC", "ITSourceCode");  
    echo ABC, "</br>";  
    echo constant("ABC");  


PHP - Constant() Function Output

The program shows that you can use either the plain echo keyword or use the echo and constant() function to display the value of the variable ABC.

As a result of this example program, the output produces two similar words.

This supports the explanation that both echo and echo with a constant function is applicable in displaying the value of a constant variable in PHP.

Constant vs Variables in PHP

Constants are comparable to variables, with the exception that once they are defined, they cannot be modified or undefined.

You can also learn the different types of variables to know more about PHP programming.

In this section, let us clarify the difference between constants and variables in PHP. The table below explains the characteristics and application of the two.

Constants are global, and once they have been defined, they cannot be redefined again. Variables may be global, static, or local, as well as undefined or redefined.
The only way to define a constant is with the define() function. It is not specified with a straightforward assignment. A variable may be defined with a straightforward assignment using the equal (=) operator.
There is no need to use the dollar sign ($) before constants in the assignment. Always use the dollar ($) sign before the variable when declaring it. 
Constants are not limited by the scope of variables, so they can be set and used anywhere. Variables can be declared at any point in a program, but there are rules about where they can be used. 
Constants are variables whose values cannot be altered when executed.Its value can be altered.
Constant vs Variables in PHP

This comparative table stresses that constants are one of the types of variables in PHP. However, this type of variable is unique since its values are unchangeable.

Meaning, that once a value is assigned to a constant variable, this value will remain unchanged throughout the execution of the project.

Remember: If the value of the variable changes or the program modifies it within the execution process, then that variable is not a constant type. That is the simple interpretation of constants in PHP from other variables.


In summary, the PHP constants are understood that they are values that cannot be changed.

This means that once a programmer applies constants in the program continuously, their value remains the same throughout its execution.

Moreover, constants are advantageous in terms of using the same value for the whole project since their application is global.

You may refer to the examples above and try them yourself to prove how they work.

Leave a Comment