PHP Constants

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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

PHP Constants: In-Depth Guide

PHP Constants

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:

  1. using the define() function
  2. 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:

  1. PHP_VERSION
  2. PHP_MAJOR_VERSION
  3. PHP_MINOR_VERSION
  4. PHP_RELEASE_VERSION
  5. PHP_VERSION_ID
  6. PHP_EXTRA_VERSION
  7. PHP_ZTS
  8. PHP_DEBUG
  9. PHP_MAXPATHLEN
  10. PHP_OS
  11. PHP_OS_FAMILY
  12. PHP_SAPI
  13. PHP_EOL
  14. PHP_INT_MAX
  15. PHP_INT_MIN
  16. PHP_INT_SIZE
  17. PHP_FLOAT_DIG
  18. PHP_FLOAT_EPSILON
  19. PHP_FLOAT_MIN
  20. PHP_FLOAT_MAX
  21. DEFAULT_INCLUDE_PATH
  22. PEAR_INSTALL_DIR
  23. PEAR_EXTENSION_DIR
  24. PHP_EXTENSION_DIR
  25. PHP_PREFIX
  26. PHP_BINDIR
  27. PHP_BINARY
  28. PHP_MANDIR
  29. PHP_LIBDIR
  30. PHP_DATADIR
  31. PHP_SYSCONFDIR
  32. PHP_LOCALSTATEDIR
  33. PHP_CONFIG_FILE_PATH
  34. PHP_CONFIG_FILE_SCAN_DIR
  35. PHP_SHLIB_SUFFIX
  36. PHP_FD_SETSIZE
  37. E_ERROR
  38. E_WARNING
  39. E_PARSE
  40. E_NOTICE
  41. E_CORE_ERROR
  42. E_CORE_WARNING
  43. E_COMPILE_ERROR
  44. E_COMPILE_WARNING
  45. E_USER_ERROR
  46. E_USER_WARNING
  47. E_USER_NOTICE
  48. E_RECOVERABLE_ERROR
  49. E_DEPRECATED
  50. E_USER_DEPRECATED
  51. E_ALL
  52. E_STRICT
  53. __COMPILER_HALT_OFFSET__
  54. true
  55. false
  56. null
  57. PHP_WINDOWS_EVENT_CTRL_C
  58. 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:

NameDescription
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.
9ClassName::classThe name of the fully qualified class.

Differences: Constants Vs Variables

Constants and variables have similarities, but there are some significant differences, too.

ConstantsVariables
1A name or an identifier for a value.A name or symbol to store a value.
2Syntax: define(name, value, case-insensitive)Syntax: Leading $ sign followed by the name of the variable (example: $name)
3The value cannot be changed once assigned.Can be reassigned a value.
4No leading $ sign is required.Must have a leading $ sign to be executed.
5By default, constants are global (universally global).Variables can be local or global.
6Useful for storing information that does not change.Useful for storing information that might change.
7Example: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.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment