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

Note: 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;
  • 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.

Example #1 Defining Constants

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

Example #2 Defining Constants using the const keyword

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



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 or if statements.

See also Class Constants.

add a note add a note

User Contributed Notes 5 notes

3 months ago
The const keyword will not allow anything that may have to be "processed":

$color = "red";
const RED = "This is the color $color"; //Doesn't work
define(strtoupper($color), "This is the color $color") // Works fine
uramihsayibok, gmail, com
5 years ago
Don't let the comparison between const (in the global context) and define() confuse you: while define() allows expressions as the value, const does not. In that sense it behaves exactly as const (in class context) does.


// this works
* Path to the root of the application
define("PATH_ROOT", dirname(__FILE__));

// this does not
* Path to configuration files
const PATH_CONFIG = PATH_ROOT . "/config";

// this does
* Path to configuration files - DEPRECATED, use PATH_CONFIG

0gb dot us at 0gb dot us
3 years ago
While most constants are only defined in one namespace, the case-insensitive true, false, and null constants are defined in ALL namespaces. So, this is not valid:

<?php namespace false;
ENT_QUOTES = 'My value';
ENT_QUOTES;//Outputs as expected: 'My value'

const FALSE = 'Odd, eh?';//FATAL ERROR! ?>

Fatal error: Cannot redeclare constant 'FALSE' in /Volumes/WebServer/ on line 5
kuzawinski dot marcin at NOSPAM dot gmail dot com
11 months ago
Actually, there is a way, to (re)declare FALSE (also: False, false, TRUE, NULL, etc.) constant, even if it is already declared:


(constant('I_DONT_EXIST')); // warning "Couldn't find constant I_DONT_EXIST"
var_dump(constant('FALSE'));  //no warning, output: bool(false)

define('FALSE', TRUE);

var_dump(constant('FALSE')); //output: bool(true)

// but...

var_dump(constant('false')); // output: bool(false)
var_dump(FALSE); // output: bool(false);

timucinbahsi at gmail dot com
3 years ago
Constant names shouldn't include operators. Otherwise php doesn't take them as part of the constant name and tries to evaluate them:

("SALARY-WORK",0.02); // set the proportion

$salary=SALARY-WORK*$work; // tries to subtract WORK times $work from SALARY
To Top