PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - Constants



A constant is an identifier (name) for a simple value. A constant value cannot change during the execution of the script. Constants are case-sensitive. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Note: Like superglobals, the scope of a constant is global. Constants can be accessed from anywhere in a script without regard to scope.

define() function

The PHP define() function is used to define a named constant. The Syntax for using this function is given below:

define(constant_name, value)

Parameters

constant_name Required. Specify name of the constant.
value Required. Specify the value of the constant. In PHP 5, value must be a scalar value (int, float, string, bool, or null). In PHP 7, array values are also accepted.

Note: Prior to PHP 8.0.0, constants defined using the define() function may be case-insensitive.

constant() function

The PHP constant() function is used to get the value of the constant. It is useful if you need to retrieve the value of a constant, but do not know its name, i.e. it is stored in a variable or returned by a function. The Syntax for using this function is given below:

constant(name)

Parameters

name Required. Specify the name of the constant.

Unlike variables, a constant does not require a $ sign. The value of a constant can be retrieved using its name. The constant() function can be used to get constant's value if it is required to obtain the constant's name dynamically.

<?php
//creating a constant
define("MAX_SIZE", 100);

//retrieving constant value
echo MAX_SIZE."\n";
//another way of retrieving constant value
echo constant("MAX_SIZE")."\n";
?>

The output of the above code will be:

100
100

Valid and invalid constant names

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

//Valid constant names
define("FOO", "something");
define("FOO2", "something else");
define("FOO_BAR", "something more");

//Invalid constant names
define("2FOO",    "something");

Constants are Global

Like superglobals, the scope of a constant is global. Constants can be accessed from anywhere in a script without regard to scope. Consider the example below:

<?php 
function CreateConstant() {
  define("MAX_SIZE", 100);
}

CreateConstant();

//retrieving constant value
echo MAX_SIZE."\n";
?>

The output of the above code will be:

100

PHP Constant Arrays

In PHP 7, an array constant can be created. Consider the example below:

<?php
//creating an array constant
define("Colors", ["Red", "Green", "Blue"]);

//retrieving values from array constant
for ($i = 0; $i < count(Colors); $i++) {
  echo Colors[$i]."\n";
}
?>

The output of the above code will be:

Red
Green
Blue

PHP Magic constants

There are nine magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script.

All these "magical" constants are resolved at compile time, unlike regular constants, which are resolved at runtime. These special constants are case-insensitive and are as follows:

NameDescription
__LINE__The current line number of the file.
__FILE__The full path and filename of the file. If used inside an include, the name of the included file is returned.
__DIR__The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__The function name, or {closure} for anonymous functions.
__CLASS__The class name. The class name includes the namespace it was declared in. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__The trait name. The trait name includes the namespace it was declared in.
__METHOD__The class method name.
__NAMESPACE__The name of the current namespace.
ClassName::classThe fully qualified class name.

5