User Contributed Notes 11 notes

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+) -- right away.

<?php

define('MIN_VALUE', '0.0'); // RIGHT - Works OUTSIDE of a class definition.
define('MAX_VALUE', '1.0'); // RIGHT - Works OUTSIDE of a class definition.

//const MIN_VALUE = 0.0; RIGHT - Works both INSIDE and OUTSIDE of a class definition.
//const MAX_VALUE = 1.0; RIGHT - Works both INSIDE and OUTSIDE of a class definition.

const MIN_VALUE = 0.0; // RIGHT - Works INSIDE of a class definition.
const MAX_VALUE = 1.0; // RIGHT - Works INSIDE of a class definition.

public static function getMinValue()
{
return self::MIN_VALUE;
}

public static function getMaxValue()
{
return self::MAX_VALUE;
}
}

?>

#Example 1:
You can access these constants DIRECTLY like so:
* type the class name exactly.
* type two (2) colons.
* type the const name exactly.

#Example 2:
Because our class definition provides two (2) static functions, you can also access them like so:
* type the class name exactly.
* type two (2) colons.
* type the function name exactly (with the parentheses).

Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).

Lets expand comment of 'storm' about usage of undefined constants. His claim that 'An undefined constant evaluates as true...' is wrong and right at same time. As said further in documentation ' 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...'. So yeah, undefined global constant when accessed directly will be resolved as string equal to name of sought constant (as thought PHP supposes that programmer had forgot apostrophes and autofixes it) and non-zero non-empty string converts to True.

There are two ways to prevent this:1. always use function constant('CONST_NAME') to get constant value (BTW it also works for class constants - constant('CLASS_NAME::CONST_NAME') );2. use only class constants (that are defined inside of class using keyword const) because they are not converted to string when not found but throw exception instead (Fatal error: Undefined class constant).

If you are looking for predefined constants like* PHP_OS (to show the operating system, PHP was compiled for; php_uname('s') might be more suitable),* DIRECTORY_SEPARATOR ("\\" on Win, '/' Linux,...)* PATH_SEPARATOR (';' on Win, ':' on Linux,...)they are buried in 'Predefined Constants' under 'List of Reserved Words' in the appendix:http://www.php.net/manual/en/reserved.constants.phpwhile the latter two are also mentioned in 'Directory Functions'http://www.php.net/manual/en/ref.dir.php

PHP Modules also define constants. Make sure to avoid constant name collisions. There are two ways to do this that I can think of.First: in your code make sure that the constant name is not already used. ex. <?php if (! defined("CONSTANT_NAME")) { Define("CONSTANT_NAME","Some Value"); } ?> This can get messy when you start thinking about collision handling, and the implications of this.Second: Use some off prepend to all your constant names without exception ex. <?php Define("SITE_CONSTANT_NAME","Some Value"); ?>

Perhaps the developers or documentation maintainers could recommend a good prepend and ask module writers to avoid that prepend in modules.

Performance of constants. PHP 7.1.10 32 bits (Opcache active, windows 10 i7-64bits) but apparently the trends is the same with the 5.x

using a constant declared by DEFINE('CNS',value) : 0.63575601577759susing a constant declared by const CNS=value : 0.61372208595276susing a variable declared by $v=value : 0.51184010505676s

In average, the use of DEFINE and CONST is around the same with some sightly better performance of CONST instead of DEFINE. However, using a variable is around 10-50% better than to use a constant. So, for a performance intensive task, constant is not the best option.