PHP Constants

PHP constants are name or identifier that can’t be changed during the execution of the script except for magic constants, which are not really constants. PHP constants can be defined by 2 ways:

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

Constants are similar to the variable except once they defined, they can never be undefined or changed. They remain constant across the entire program. PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only.

Conventionally, PHP constants should be defined in uppercase letters.

Note: Unlike variables, constants are automatically global throughout the script.

PHP constant: define()

Use the define() function to create a constant. It defines constant at run time. Let’s see the syntax of define() function in PHP.

  1. define(name, value, case-insensitive)  
  1. name: It specifies the constant name.
  2. value: It specifies the constant value.
  3. case-insensitive: Specifies whether a constant is case-insensitive. Default value is false. It means it is case sensitive by default.

Let’s see the example to define PHP constant using define().

<?php  
define("MESSAGE","Hello JavaTpoint PHP");  
echo MESSAGE;  
?>  

Output:

Hello JavaTpoint PHP

Create a constant with case-insensitive name:

<?php    
define("MESSAGE","Hello JavaTpoint PHP",true);//not case sensitive    
echo MESSAGE, "</br>";    
echo message;    
?>    

Output:

Hello JavaTpoint PHP
Hello JavaTpoint PHP
<?php  
define("MESSAGE","Hello JavaTpoint PHP",false);//case sensitive  
echo MESSAGE;  
echo message;  
?>  

Output:

Hello JavaTpoint PHP
Notice: Use of undefined constant message - assumed 'message' 
in C:\wamp\www\vconstant3.php on line 4
message

PHP constant: const keyword

PHP introduced a keyword const to create a constant. The const keyword defines constants at compile time. It is a language construct, not a function. The constant defined using const keyword are case-sensitive.

<?php  
const MESSAGE="Hello const by JavaTpoint PHP";  
echo MESSAGE;  
?>  

Output:

Hello const by JavaTpoint PHP

Constant() function

There is another way to print the value of constants using constant() function instead of using the echo statement.

Syntax

The syntax for the following constant function:

  1. constant (name)  
<?php      
    define("MSG", "JavaTpoint");  
    echo MSG, "</br>";  
    echo constant("MSG");  
    //both are similar  
?>  

Output:

JavaTpoint
JavaTpoint

Constant vs Variables

ConstantVariables
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.

Javascript Constant – const Keyword

JavaScript const keyword is used to define constant values that can not changed once a value is set. The value of a constant can’t be changed through reassignment, and it can’t be redeclared.

The scope of const is block-scoped it means it can not be accessed from outside of block. In case of scope, it is much like variables defined using the let statement.

Constants can be either global or local to the block in which it is declared. Global constants do not become properties of the window object, unlike var variables.

JavaScript const Keyword: Syntax

Below we have the syntax to define a constant value in JavaScript.

const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]]

We don’t have to use var or let keyword while using the const keyword. Also, we can define a list or a simple value or a string etc as a constant.

JavaScript const Keyword Example

Lets understand, how to create constant in JavaScript program. See the below example:

{
	const Pi = 3.14;
	alert(Pi);
}

3.14

Let’s try another example where we will try changing the value of the constant and see if we allowed to reassign value or not.

{
	const Pi = 3.14;
	alert(Pi);
	
	// Reassign value
	Pi = 3.143;
	alert(Pi);
}

3.14 Uncaught TypeError: Assignment to constant variable.

JavaScript const: Scope

The scope of the variable defined using const keyword is same as that of a variable declared using the let keyword. So, constant declared inside a block will not accessible outside of that block. Let’s take an example and see:

{
	const Pi = 3.14;
	alert(Pi);
}

// outside block
alert(Pi);  // error

3.14 Uncaught ReferenceError: Pi is not defined

JavaScript const Keyword Usage Example

In the live example below we have added multiple code statements in which we have declared constant values and have given it different scope and have also tried to change the value of the constant.

/* CSS comes here */ const Statement

Understand use of const in JavaScript

// global scope const x = 200 // block scope { const x = 10 document.write(“Inside the block:”+x) } // function scope function show() { const x = 20; document.write(“
Inside the function:”+x) } show(); document.write(“
Global x:”+x) x = 100 // TypeError: Assignment to constant variable. document.write(“
Global x:”+x)

To see the error, you can open the browsers developer tools(If you are using Chrome or Firefox browser), you will see the Uncaught TypeError: Assignment to constant variable error message in the console.

So this is how we can define a constant in JavaScript which can be used in cases where you want to define some constant values like domain name, any prefix value which is fixed, or any integer value, or may be a zipcode in your JavaScript code.