PHP Constants

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Example #1 Valid and invalid constant names

<?php 
 // Valid constant names
  define("FOO", "something);
  define("FOO2", "something else);
  define("FOO_BAR", "something);
 // Invalid constant names
  define("2FOO", "something);
?>

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+)

PHP Constant: define()

define (name, value, case-insensitive) //syntax

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

Below the example of create a constant with a case-sensitive name:

<?php 
 define("MESSAGE", "Hello,World");
 echo MESSAGE; // Output  Hello,World
?>

Below the example of create a constant with a case-insensitive name:

<?php 
 define("MESSAGE", "Hello,World", true);
 echo MESSAGE; // Output  Hello,World
 echo message; // Output  Hello,World
?>

PHP Constant: const keyword

Below the example of create a constant using const keyword

const CONSTANT_NAME = "constant value";

You can define constants in the class. Value of the constant will remain unchanged.Value must be a constant value and can not be a variable, class, property

Class constants are always case-sensitive.

Below the example of use of const keyword

<?php 
 class Constantclass
  {
         const MSG = "Hello, World";
  }
   echo Constantclass::MSG; // Output Hello, World
?>

Difference between const vs define

As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function:

const FOO = 'BAR';
define('FOO', 'BAR');

const defines constants at compile time, whereas define defines them at run time. This causes most of const's disadvantages. Some disadvantages of const are:

  • const cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:
  • if (...) {
        const FOO = 'BAR';    // invalid
    }
    // but
    if (...) {
        define('FOO', 'BAR'); // valid
    }
  • const takes a plain constant name, whereas define() accepts any expression as name.
  • consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:
  • define('FOO', 'BAR', true);
    echo FOO; // BAR
    echo foo; // BAR
  • Since PHP 5.6 const constants can also be arrays, while define() does not support arrays yet. However arrays will be supported for both cases in PHP 7.

    const FOO = [1, 2, 3];    // valid in PHP 5.6
    define('FOO', [1, 2, 3]); // invalid in PHP 5.6, valid in PHP 7.0
  • As consts are language constructs and defined at compile time they are a bit faster than define()