PHP Constants


PHP Constants

 

PHP Constants are similar to PHP Variables but they have a fixed value that can’t change during the execution of the PHP script.  We can define constants in different two ways:

define () function: It define constant at run time.

Syntax:

define(name, value, case-insensitive);

Parameters:

  •  name:  It Specifies the name of the constant.
  •  value: It Specifies the value of constant.
  •  case-insensitive: Its default value is false.

Example 1:

php constants, define function

Output:

php constants, case sensitive

Example 2:

php constants, define function

Output:

php constants, in case sensitive

const keyword: It defines constant at compile time.

It defines case-sensitive constants.

Example: 

php constants, const keyword

Output: 

php constants, const keyword output

constant() function: This is another way of print the values of constants.

Syntax:

constant (constant_name);

Example: 

php constants, constant funtion

Output:

php constants, output constant function

 

Read Also: Difference between Variable and Constant