PHP Constants

A PHP constant works same as a PHP Variable in terms of storing a value, but the limitation with PHP constants is that the value cannot be changed after getting assigned. In PHP variables, we can manipulate the values by doing different mathematical operations but PHP constants doesn't allow manipulating the value.

In this tutorial we will go through an In-depth analysis og PHP Constants, we will talk about different ways of creating constants, properties of constants. Let's start!


Naming a Constant

The naming conventions for a constant are similar to that of a variable. A valid constant name can be started with a letter or you can also use an underscore in the starting or anywhere in between the identifier, followed by any letter/numbers/underscore.

Note: The '$' sign is not used in PHP Constants. You simply can use an identifier to represent a Constant without using the '$' sign.
Note: Constants are by default Global in PHP, i.e., they can be used anywhere in the program, without having any limitation of scope.

How to define PHP Constants:

A PHP constant can be defined by using define() function and to retirieve the value of a constant you can simple use the echo() construct. The constant() construct can also be used to read the value of a constant, and it faster than echo() but we will go through this later in the tutorial. Let's first see how you can create the PHP constants using define() function.


PHP Constant using define()

The define() function uses three arguments: Constant name, Constant value, Case insensitivity(TRUE/FALSE). The Case-insensitivity is by default FALSE in the function, i.e., if you don't specify the insensitivity as TRUE then the Parser will take it as FALSE and the Constant will be case sensititive, which means that uppercase and lowercase will be treated differently.

Look at the example below to understand how to create a PHP constant by using define() function and then the insensitivity works:

Syntax
define(name, value, case-insensitivity)

Parameters:

  • name: Specifies the name of the constant.
  • value: Specifies the value of the constant.
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

Case-Sensitive Constants:

Example: 1

<?php /* case-sensitive constant name */ define("GREETING", "Welcome to PHP Tutorials"); echo GREETING; ?>

Output

Welcome to PHP Tutorials

In the example above, we have not specified the insensitivity within the define() function, so the PHP Parser took the default value 'FALSE'.

Example: 2

<?php /* case-sensitive constant name */ define("GREETING", "Welcome to PHP Tutorials"); echo greeting; ?>

Output

greeting

Now, the constant is case-sensitive,hence 'GREETING' is treated as a constant but 'greeting' is just a string.


Case In-Sensitive Constants:

Example

<?php /* case-insensitive constant name */ define("GREETING", "Welcome to PHP Tutorials", true); echo greeting; ?>

Output

Welcome to PHP Tutorials

In the example above, we have specified the insensitivity as 'TRUE'. Now 'GREETING' and 'greeting' or 'Greeting' will all be treated as a constant and will produce the same result.


PHP constants with constant() language construct

The constant() is a language construct just like echo(). It is used to read a constant's value when you don't know the name of the constant ,i.e., it is stored in a variable or returned by any function. In this case you can use the constant() construct and it also works with class constants. Look at the example below to understand the working of constant().

Note: It is always case-sensitive, so use it carefully and if the constant is not defined then it returns NULL.
Example

This is example of constant() <?php $str= "constName"; define("constName","This is constant"); echo constant($str); ?>

Output

This is constant

In this example above, we used the variable name which holds the name of the constant but still got the constant's value. In this way we can retireve values of constants whose names we don't know or to be more precise we can read the values dynamically during runtime.


In this example we have shown how you can use the constant() construct to access the constant values present inside a class.

Example

/* this is example for constant() with class */ <?php class a { const b = 'c'; } echo constant('a::b'); ?>

Output

c

Global Constants

PHP Constants are by default Global in nature. In the example below a constant is defined outside a function but still can be accessed from the inside of the function, i.e., the Constant is Global in nature.

Example

<?php define("GREETING", "Welcome to PHP Tutorials"); function myTest() { echo GREETING; } myTest(); ?>

Output

Welcome to PHP Tutorials

Constant vs Variables

Constant Variables
Once the constant is defined, it can never be redefined. A variable can be undefined as well as redefined easily.
A constant can only be defined using define() function. It cannot be defined by any simple assignment. A variable can be defined by simple assignment (=) operator.
There is no need to use the dollar ($) sign before constant during the assignment. To declare a variable, always use the dollar ($) sign before the variable.
Constants do not follow any variable scoping rules, and they can be defined and accessed anywhere. Variables can be declared anywhere in the program, but they follow variable scoping rules.
Constants are the variables whose values can't be changed throughout the program. The value of the variable can be changed.
By default, constants are global. Variables can be local, global, or static.










Follow Us: