PHP Constants

PHP constants are similar like constant variables in other programming languages. A PHP constant holds a fixed value, which cannot be updated after declaration.

PHP Constants

  • PHP Constants are similar to the variables, and they cannot be changed or redefined after once being created and assigned a value.
  • They remain constant across the entire program.
  • It is useful for storing data that doesn’t change while the PHP program is running.
  • Examples of such data are configuration settings such as database username and password, website base URL, etc.
  • We can define constants using PHP’s define () function, which contains two arguments, the name of the constant and its value.
  • Once we determine, the constant value can retrieve at any time by referring to its name.

Read Also: PHP Data Types

Naming Conventions for PHP Constants

  • Constant name follows the same rules as variable names.
  • A valid constant name must start with a letter or underscore followed by several letters etc.
  • $ Prefix is not necessary for the constant name.
  • PHP Constants define in UpperCase letters. (Ex. ABC, XYZ)

PHP Constant Declaration Methods

We can define a constant in PHP using two methods. However, both methods work the same. So, there will be no difference in the output or usage.

  1. Using define() function
  2. Using const keyword

PHP Constant Declaration Using define() Function

To create a PHP constant, we can use the define() function. For this, we have to declare its name and value, which we won’t be able to change later. It must begin with a letter or underscore (_) and contain as many letters or numbers.

Syntax

define(name, value, case-insensitive);
  • Name: It specifies the constant name.
  • Value: It specifies the constant value.
  • Case Insensitive: It is a boolean value (TRUE/FALSE), which specifies whether a constant is case insensitive or not.

Example 1

<?php

define("MSG", "Hello Suman");

echo MSG;

?>

Output

Hello Suman

Example 2

Constant with case-insensitive name

<?php

define("MSG","Hello Suman",true);//not case sensitive

echo MSG, "</br>";
echo message;

?>

Output

Hello Suman

Hello Suman

PHP Constant Declaration Using the Const Keyword

The const keyword defines a constant at compile time. The const keyword is not a function; it is a language construct. Constants defined using the const keyword are case sensitive.

Example

<?php

const MSG="Hello Suman";

echo MSG;

?>

Output

Hello Suman

Global Constants

In PHP, constants are global and used across the script.

Example

<?php

define("HELLO", "Welcome Suman");

function myTest() {

    echo HELLO;

}

myTest();

?>

Output

Welcome Suman

Constant() Function

It is another way to print the value of constants using constant() function instead of using the echo statement. Constant() method returns the constant value, and NULL value if the constant is not defined.

Syntax

constant(name);

Example

<?php

define("MSG", "Suman");

echo MSG, "</br>";

echo constant("MSG");

//both are similar

?>

Output

Suman

Suman

Difference Between Constant and Variable

  • In variable, the dollar sign ($)  before the variable is mandatory, whereas in constant, there is no need to write a dollar sign.
  • Once the constant is defined, it can never redefine, whereas a variable can be undefined as well as redefined easily.
  • A constant can only be defined using a define() function, whereas a variable set using a simple assignment (=) operator.
  • Constants are global by default, whereas variables can be Local, Global or Static.
  • Constants are the variables whose value can’t be changed throughout the program, while the value of the variable can be changed.
  • The constants do not follow any variable scoping rules, and they can be defined & accessed anywhere. In contrast, on the other side, the variable can be declared anywhere, but they follow the variable scoping rules.

Read More: PHP Operators

Conclusion

Constants are useful for storing data that doesn’t need to change while the PHP program is running.

I hope you have a complete understanding of PHP Constants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
PHP Data Types
PHP Data Types

For constructing variables, PHP supports various data types. A Data type is an attribute of data which tells the compiler...

Close