PHP Constants

In PHP, a constant is like a variable except for one fact its value cannot be changed once it has been created. A valid constant name can be started with a letter or an underscore(_). There is no need to put the dollar sign($), as we do with a variable.

Note: A constant is global throughout the script.

How to create a constant in PHP?

We use the define function or the ‘const’ keyword to create a constant in PHP.

Syntax of define function:

define(contant name, constant value, case sensitivity);
  • Constant name: It is the name of the constant to be declared.
  • Constant value: It is the value that is to be assigned to the newly created constant.
  • Case sensitivity: It is a good practice to capitalize the name of a constant. For example, ADDRESS. But it is not restricted to doing so. Case sensitivity defines whether the constant name would be case sensitive or not. By default, the name of a constant is case-sensitive and the value of this argument is false.

Example:

<?php
    define('HOBBY', 'Painting');
    echo HOBBY;
?>

Output:

Painting

This is to be noted that the constant name in the above example is case-sensitive. That means we cannot use it like this-

echo hobby; //(Incorrect)

How to create a case-insensitive constant in PHP?

To create a case insensitive constant, we simply put the third argument in the define function as true.

<?php 
    define('HOBBY', 'Painting', true); 
    //Now the constant name is case insensitive. We can use its name in any case we wish. 
    echo Hobby;  
?>

Value of a constant is not changeable

If you try to change the value of a predefined constant, you will get an error.

<?php
	define('HOBBY', 'Painting');
    	echo HOBBY;
    	define('HOBBY', 'Swimming');
    	echo HOBBY;
?>

This code will generate a notice error saying that the constant has already been defined.

How to Create an array constant?

This was not possible in the previous versions of PHP but, in PHP7, we can create an array constant by using the define function.

Example:

<?php
    define("COLORS", ['red', 'blue', 'green']);
    echo COLORS[1];
?>

Output:

blue

Constants are always global

As I have said above, a constant is always global in a script and can be used in any scope.

Example:

<?php
    define("HOBBY", "Painting");

    function constantTest() {
        echo "My hobby is ". HOBBY;
    }
    constantTest();
?>

Output:

My hobby is Painting

How to Define a constant with the const keyword?

The const keyword is another way to create a constant in PHP.

Example:

<?php
    const HOBBY = 'Painting';
    echo HOBBY;
?>

Output:

Painting

The const keyword works at the compile time, that is why you cannot place it inside a conditional statement.

<?php
    $a = 1;
    if($a == 1){
        const HOBBY = 'Painting';        
    }
    echo HOBBY;
?>

The above code will generate a syntax error.

Differences between the define function and the const keyword

  1. The define function can be used from PHP version 4 to all above the versions, whereas the const keyword cannot be used in PHP versions below 5.3.0.
  2. Constants created with the const keyword are always case sensitive, whereas with the define function, you can create a case-insensitive constant by passing a third argument as true.
  3. The const keyword creates a constant at the compile time, whereas the define function works at the at the run time.
  4. The const keyword can be used inside a class to create a class constant, while define cannot be used in this context.