You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.

Only scalar data (boolean, integer, float and string) can be contained in constants prior to PHP 5.6. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

You can get the value of a constant by simply specifying its name. Unlike with variables, you should not prepend a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants.

Зауваження: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function.

These are the differences between constants and variables:

  • Constants do not have a dollar sign ($) before them;
  • Prior to PHP 5.3, Constants may only be defined using the define() function, not by simple assignment;
  • Constants may be defined and accessed anywhere without regard to variable scoping rules;
  • Constants may not be redefined or undefined once they have been set; and
  • Constants may only evaluate to scalar values. As of PHP 5.6 it is possible to define array constant using const keywords and as of PHP 7 array constants can also be defined using define() You may use arrays in constant scalar expressions (for example, const FOO = array(1,2,3)[0];), but the end result must be a value of allowed type.

Приклад #1 Defining Constants

("CONSTANT""Hello world.");
CONSTANT// outputs "Hello world."
echo Constant// outputs "Constant" and issues a notice.

Приклад #2 Defining Constants using the const keyword

// Works as of PHP 5.3.0
const CONSTANT 'Hello World';


// Works as of PHP 5.6.0
const ANOTHER_CONST CONSTANT.'; Goodbye World';

ANIMALS = array('dog''cat''bird');
ANIMALS[1]; // outputs "cat"

// Works as of PHP 7
define('ANIMALS', array(
ANIMALS[1]; // outputs "cat"


As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks.

See also Class Constants.

add a note add a note

User Contributed Notes 4 notes

login at (two)view dot de
6 years ago
Just a quick note:
From PHP7 on you can even define a multidimensional Array as Constant:

define('QUARTLIST',array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul'));

does work as expected.
halfhearted13 at gmail dot com
3 years ago
Constants of constant :


const foo = hello;
bar = foo;
bd = bar;

"bd = " .bd;   // Prints out: Hello

privat at ulrich-kapp dot de
3 years ago
const ArrayFromTextfile = file("mytextfile.txt", FILE_IGNORE_NEW_LINES);

does not work, it throws an error:
Fatal error: Constant expression contains invalid operations in php shell code on line …

Instead use:

define ("ArrayFromTextfile", file("mytextfile.txt", FILE_IGNORE_NEW_LINES));


    [0] ⇨ Line 1
    [1] ⇨ Line 2
    [2] ⇨ Line 3
    [3] => …
mh at hanft dot de
4 years ago
With PHP 5.6, multi-dimensional arrays are also possible if you use "const" instead of "define". So,

define('QUARTLIST',array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul')));

won't work with PHP 5.6, but

const QUARTLIST=array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul'));

To Top