Last Updated:
Back to Main Index

A constant is a simple way to define a more complex value for reuse throughout your project.

  • A constant value cannot change during the execution of the script.
  • By default a constant is case-sensitive.
  • By convention, constant identifiers are always uppercase.
  • A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
  • If you have defined a constant, it can never be changed or undefined without rewriting everything connected with it.
  • Only scalar data (boolean, integer, float and string) can be contained in constants.

While they sound a lot like variables, there are a few differences between constants and variables to help you determine when you should use either:

  • There is no need to write a dollar sign ($) before a constant
  • Constants cannot be defined by simple assignment, they may only be defined using the define() function, and only once
  • Constants may be defined and accessed anywhere without regard to variable scoping rules (ie no need to global a constant)

To define a constant, use the define() function. To retrieve the value of a constant, simply inert the constant name wherever the value should output. You can also use the function constant() to read a constant’s value if you wish to obtain the constant’s name dynamically.

Valid and invalid constant names: