PHP Constants are the identifiers that cannot be changed. Learn how to create a PHP Constant, and know more about pre-defined constants & Magic constants with examples:
In this tutorial, you will learn what a PHP constant is, how to create PHP constants (using the PHP define() function and the const keyword), predefined constants, magic constants, differences between constants and variables, and frequently asked questions (FAQs).
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> In-Depth PHP Tutorials for Beginners
Table of Contents:
PHP Constants: In-Depth Guide

A PHP constant is a name for a simple value. Unlike variables, we cannot change constants while running the script.
The characteristics of constants are:
- A constant name starts with a letter or _ (the underscore character).
- Unlike variables, no leading $ sign.
- By default, constant names are case sensitive (we can make them case insensitive while defining).
- The scope is global.
How to Create a PHP Constant
In PHP, there are two ways to define a constant, as shown in the list below:
- using the define() function
- using the const keyword
#1) Using the PHP define() Function
The syntax is as follows:
define(name, value, case-insensitive)
The above syntax has three parameters: name (required parameter), value (required parameter), and case-insensitivity. The Default value of case-insensitivity is false and is only required if you create a case-insensitive constant name.
A few valid constant names are:
- define(“FOO”, “some text”);
- define(“FOO2”, “some text”);
- define(“FOO_BAR”, “some text”);
An invalid constant name includes:
- define(“2FOO”, “some text”);
It is invalid, as the name cannot start with a number.
Let’s look at some examples.
You can practice these examples by running the following programming codes.
Example 1:
<?php
define("WELCOME", "Welcome to My World!");
echo WELCOME;
?>
Output:
Welcome to My World!
Example 2: An array constant
Note: We will cover arrays in a later tutorial.
<?php
define("colors", ["Red","Yellow","Green"]);
echo colors[0];
?>
Output:
Red
#2) Using the const Keyword
The syntax is as follows:
const NAME = value;
In the above syntax, NAME is not case-sensitive but is usually written in uppercase.
Let’s look at some examples.
You can practice these examples by running the following programming code.
Example 1:
<?php
const MY_NUM = 7;
echo MY_NUM;
?>
Output:
7
Example 2:
<?php
class MyPet {
const NAME = "Tommy";
}
echo MyPet::NAME;
?>
Output:
Tommy
Core Predefined Constants
They are defined by the PHP core. There are 58 predefined constants, as shown in the list below:
- PHP_VERSION
- PHP_MAJOR_VERSION
- PHP_MINOR_VERSION
- PHP_RELEASE_VERSION
- PHP_VERSION_ID
- PHP_EXTRA_VERSION
- PHP_ZTS
- PHP_DEBUG
- PHP_MAXPATHLEN
- PHP_OS
- PHP_OS_FAMILY
- PHP_SAPI
- PHP_EOL
- PHP_INT_MAX
- PHP_INT_MIN
- PHP_INT_SIZE
- PHP_FLOAT_DIG
- PHP_FLOAT_EPSILON
- PHP_FLOAT_MIN
- PHP_FLOAT_MAX
- DEFAULT_INCLUDE_PATH
- PEAR_INSTALL_DIR
- PEAR_EXTENSION_DIR
- PHP_EXTENSION_DIR
- PHP_PREFIX
- PHP_BINDIR
- PHP_BINARY
- PHP_MANDIR
- PHP_LIBDIR
- PHP_DATADIR
- PHP_SYSCONFDIR
- PHP_LOCALSTATEDIR
- PHP_CONFIG_FILE_PATH
- PHP_CONFIG_FILE_SCAN_DIR
- PHP_SHLIB_SUFFIX
- PHP_FD_SETSIZE
- E_ERROR
- E_WARNING
- E_PARSE
- E_NOTICE
- E_CORE_ERROR
- E_CORE_WARNING
- E_COMPILE_ERROR
- E_COMPILE_WARNING
- E_USER_ERROR
- E_USER_WARNING
- E_USER_NOTICE
- E_RECOVERABLE_ERROR
- E_DEPRECATED
- E_USER_DEPRECATED
- E_ALL
- E_STRICT
- __COMPILER_HALT_OFFSET__
- true
- false
- null
- PHP_WINDOWS_EVENT_CTRL_C
- PHP_WINDOWS_EVENT_CTRL_BREAK
The list below shows more details about selected predefined constants.
- PHP_VERSION
- Description – The current version of PHP within the format of major.minor.release[extra].
- Output data type – string
- PHP_MAJOR_VERSION
- Description – The current major version of PHP.
- Output data type – int
- Ex: 7 from version 7.2.0
- PHP_MINOR_VERSION
- Description – The current minor version of PHP.
- Output data type – int
- Ex: 2 from version 7.2.0
- PHP_RELEASE_VERSION
- Description – The current release version of PHP.
- Output data type – int
- Ex: 0 from version 7.2.0
- PHP_VERSION_ID
- Description – The current version of PHP. It is useful when comparing versions.
- Output data type – int
- Ex: 70200 from version 7.2.0
- PHP_EXTRA_VERSION
- Description – The current extra version of PHP. Useful to specify a package version.
- Output data type – string
- PHP_DEBUG
- Description – Assist in debugging PHP code.
- Output data type – int
- PHP_MAXPATHLEN
- Description – The maximum length of file names (including path) is supported by this PHP build.
- Output data type – int
- PHP_OS
- Description – The operating system (OS) PHP was built for.
- Output data type – string
- PHP_OS_FAMILY
- Description – The OS family PHP was built for.
- Output data type – string
- Available since PHP 7.2.0
- PHP_INT_MAX
- Description – The largest integer supported during this PHP build.
- Output data type – int
- PHP_INT_MIN
- Description – The smallest integer supported during this PHP build.
- Output data type – int
- PHP_INT_SIZE
- Description – The size of an integer in bytes during this PHP build.
- Output data type – int
- PHP_FLOAT_MIN
- Description – Smallest representable positive floating-point number.
- Output data type – float
- Available since PHP 7.2.0
- PHP_FLOAT_MAX
- Description – The largest representable floating-point number.
- Output data type – float
- Available since PHP 7.2.0
- E_ERROR
- Description – Fatal run-time errors.
- Output data type – int
- E_WARNING
- Description – Run-time warnings (non-fatal errors)
- Output data type – int
- E_PARSE
- Description – Compile-time parse errors.
- Output data type – int
- E_NOTICE
- Description – Run-time notices.
- Output data type – int
- E_ALL
- Description – All errors and warnings (as supported).
- Output data type – int
Magic Constants
PHP provides a special set of predefined constants called magic constants. They can change depending on the place where they are used. They start with two underscores and also end with two underscores. Furthermore, magic constants are case-insensitive.
The following table shows the magic constants in PHP:
| Name | Description | |
|---|---|---|
| 1 | __LINE__ | The line number of the current file. |
| 2 | __FILE__ | The full path of the file and the file name with symbolic links resolved. If it uses inside an include, the included file name will return. |
| 3 | __DIR__ | The directory of the file. If it uses inside an include, the included file directory will return. It is equivalent to dirname(__FILE__). If it is not the root directory, it will not contain a trailing slash. |
| 4 | __FUNCTION__ | The name of the function. |
| 5 | __CLASS__ | The name of the class. It includes the namespace it was declared in (Ex: Foo\Bar). If it is used in a trait method, __CLASS__ is the class name the trait is used in. |
| 6 | __TRAIT__ | The name of the trait. It includes the namespace it was declared in (Ex: Foo\Bar). |
| 7 | __METHOD__ | The name of the class method. |
| 8 | __NAMESPACE__ | The name of the current namespace. |
| 9 | ClassName::class | The name of the fully qualified class. |
Differences: Constants Vs Variables
Constants and variables have similarities, but there are some significant differences, too.
| Constants | Variables | |
|---|---|---|
| 1 | A name or an identifier for a value. | A name or symbol to store a value. |
| 2 | Syntax: define(name, value, case-insensitive) | Syntax: Leading $ sign followed by the name of the variable (example: $name) |
| 3 | The value cannot be changed once assigned. | Can be reassigned a value. |
| 4 | No leading $ sign is required. | Must have a leading $ sign to be executed. |
| 5 | By default, constants are global (universally global). | Variables can be local or global. |
| 6 | Useful for storing information that does not change. | Useful for storing information that might change. |
| 7 | Example: | Example: |
| define(‘TEST’, ‘Software Testing Help’) | $test = ‘Software Testing Help’ |
Frequently Asked Questions
1. What are PHP constants?
Constants are names used for simple values. Furthermore, they cannot be changed while running the script.
2. How is a PHP constant created?
There are two methods to create a constant. They are using the PHP define() function and using the const keyword.
3. Give an example of a PHP constant.
Please refer to the example below:
const TEST = ‘Software Testing Help’;
(OR)
define(‘TEST’, ‘Software Testing Help’)
4. Can we redefine a constant in PHP?
No, unlike variables, we cannot redefine a constant once it is defined.
5. Show in an example how a function can access a constant that is defined outside the function.
A function can easily access a constant as constants have a global scope. Please refer to the example below:
<?php
define(“GREETING”, “Have a nice day!”);
function myExample() {
echo GREETING;
}
myExample();
?>
It will echo the following output:
Have a nice day!
6. What is the main difference between const and define in PHP?
The main difference is that const defines a constant at compile-time, whereas define() defines a constant at run-time.
Conclusion
A PHP constant is a name for a simple value and cannot be changed later on. The PHP define() function or the const keyword is used to create a constant.
Unlike variables, constants are accessible from anywhere in the script. Furthermore, there are predefined constants as well as magic constants in PHP.






