PHP Constants

Summary: in this tutorial, we will show you how to define PHP constants. In addition, we will introduce you some commonly used built-in constants.

Introduction to PHP constants

PHP ConstantsThe value of a PHP constant, like its name implies, cannot be changed during the execution of the script. To define a constant in PHP, you use the  define() function. The  define() function takes the name of the constant as the first argument and the contant value as the second argument.

Unlike a variable, the name of a constant does not begin with the dollar sign ( $).

A constant can hold only a scalar or a simple value such as integer, float, string or Boolean. Once a constant is defined, it can be accessible from anywhere in the script.

This is an example of defining a constant:

We defined a constant named MESSAGE that stores a string literal. By convention, constants are in uppercase.

Notice that you cannot redefine or unset a constant like a variable. In practice, you often use constants to store values that do not change throughout the application e.g., labels for a button, etc.

Getting value of a constant through a variable

Sometimes, you want to get the value of a constant but you do not know its name e.g., a constant value stored in a variable or returned by a function.

Fortunately, PHP provides the constant() function that accepts the constant name and returns the constant value. If the constant has not yet defined, the constant() function returns null.

The following example illustrates how to use the constant() function:

The code looks cryptic if you don’t know anything about function and switch statement. Just take a look at the last line of the script where we used the constant() function. You can also revisit this example after going through the entire PHP basic tutorial.

Checking if a constant is defined

Sometimes, it is very useful to check if a constant is defined before using it. To check if a constant is defined, you use the defined() function.

The defined() function returns true if the constant exists, otherwise it returns false.

Notice that we used the if else statement to check whether the MAX_SIZE constant defined or not.

PHP predefined magic constants

PHP provides some useful predefined magic constants which start and end with double underscores ( __). For example, the __FILE__ constant holds the name of PHP file that’s being executed and the __LINE__ constant holds the current line number of the file.

The other PHP predefined magic constants are listed as follows:

  • __DIR__ holds the directory of the current script file.
  • __FUNCTION__  holds the current function name.
  • __CLASS__ holds the current class name including namespace.
  • __TRAIT__ hold the current trait name.
  • __METHOD__ holds the current method name of a class.
  • __NAMESPACE__  holds the name of the current namespace.

In this tutorial, you have learned how to define and use PHP constants in your script.

  • Was this tutorial helpful ?
  • YesNo