PHP Constants

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.

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

 

PHP constants can be defined by 2 ways:

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

 

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

<?php  

  const MESSAGE = "Hello Mates Welcome to Elevenstech"; 
 
  echo MESSAGE;  

?>  

Output:

Hello Mates Welcome to Elevenstech

 

Create a PHP Constant

To create a constant, use the define() function.

Syntax: define(name, value, case-sensitive)

 

Parameters:

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

 

Example:

Create a constant with a case-sensitive name:

<?php

  define("WELCOME", "Welcome to elevenstechwebtutorials.com");

  echo WELCOME;

?>

Output : Welcome to elevenstechwebtutorials.com

 

Example:

Create a constant with a case-insensitive name:

<?php

  define("WELCOME", "Welcome to elevenstechwebtutorials.com", true);

  echo welcome;

?>

Output : Welcome to elevenstechwebtutorials.com

 

PHP Constant Arrays

In PHP7, you can create an Array constant using the define() function.

Example:

Create an Array constant:

<?php

  define("message", [
    "Welcome",
    "to",
    "Elevenstech"
  ]);

  echo message[2];
?>

Output : Elevenstech

 

Constants are Global

Constants are automatically global and can be used across the entire script.

Example:

This example uses a constant inside a function, even if it is defined outside the function:

<?php

   define("WELCOME", "Welcome to elevenstechwebtutorials.com");

   function myFirstCode() 
   {
     echo WELCOME;
   }
 
   myFirstCode();

?>

Output : Welcome to elevenstechwebtutorials.com

 

Difference between PHP Constants and PHP Variables

PHP Constants:

  1. In PHP constants there is no need to use $ sign.
  2. A PHP constant once defined cannot be redefined.
  3. We can not define a constant using any simple assignment operator rather it can only be defined using define().
  4. The data type of PHP constant cannot be changed during the execution of the script.
  5. PHP constants are automatically global across the entire script.
  6. Usually, constants are written in numbers.
  7. PHP constant is comparatively slower than PHP variable


PHP Variables:

  1. In PHP Variables the $ sign is been used.
  2. A PHP variable can be undefined as well as can be redefined.
  3. The data type of the PHP variable can be changed during the execution of the script.
  4. On the other hand, variables are written in letters and symbols.
  5. PHP variables are not automatically global in the entire script.
  6. We can define a variable using a simple assignment operation(=).
  7. A PHP variable is comparatively faster than the PHP constant