Please wait

Constants

A constant in PHP is a named value that cannot be changed or modified during the execution of the program. Think of a constant as a fixed value that you can use in your code, like a number written on a piece of paper that can never be erased or altered.

Why use constants?

For example, you can define a constant named PI with the value 3.14, and then use that constant in your code to perform calculations that require the value of pi. Because constants cannot be changed, you can be sure that the value of PI will always be 3.14 in your code, no matter what.

Constants are useful in PHP because they allow you to define values that are used frequently or have a fixed value, making it easier to maintain your code and reduce the risk of errors. By using constants, you can make your code more readable, easier to understand, and less prone to errors caused by typos or accidental changes.

The const keyword

In PHP, you can define constants using the const keyword. The general syntax for defining a constant is as follows:

const CONSTANT_NAME = value;

Where CONSTANT_NAME is the name of the constant and value is the value you want to assign to it. The constant name must be a valid PHP identifier and start with a letter or an underscore. The value can be a string, a number, or any other scalar data type.

For example:

const PI = 3.14;
const NAME = "John Doe";
const IS_ADMIN = true;

Once a constant is defined, you can access its value anywhere in your code simply by referencing its name like this:

echo PI;
echo NAME;
echo IS_ADMIN;

Naming Conventions

There are two things you'll notice with constant names. Firstly, constants don't need the $ character at the beginning of their name. Secondly, constants have naming conventions that are similar to those of variables but with some differences.

Here are some of the common naming conventions for constants in PHP:

  1. Uppercase letters: It is a common practice to use uppercase letters for the names of constants as a way to differentiate them from variables, which are typically named with lowercase letters. For example: const PI = 3.14;.
  2. Underscore separators: To separate words in the constant name, it is common to use underscores. This helps make the constant name more readable and easier to understand. For example: const MAX_ATTEMPTS = 5;.
  3. Predictable and meaningful names: As with variables, it's important to choose meaningful and predictable names for constants that accurately reflect the value that they hold. For example, const NAME = "John Doe"; is a clearer and more meaningful name than const X = "John Doe";.

These naming conventions are not strict rules but rather guidelines that are widely followed by the PHP community. By following these conventions, you can make your code more readable, maintainable, and understandable to others who may work on it in the future. It also helps other developers identify constants in your script.

The define function

There's an alternative solution for defining constants, which is by using the define() function. The general syntax for defining a constant using define() is as follows:

define("CONSTANT_NAME", value);

Where CONSTANT_NAME is the name of the constant and value is the value you want to assign to it. The constant name must be a string and start with a letter or an underscore. The value can be a string, a number, or any other scalar data type.

define("PI", 3.14);
define("NAME", "John Doe");
define("IS_ADMIN", true);

Once a constant is defined, you can access its value anywhere in your code simply by referencing its name like this:

echo PI;
echo NAME;
echo IS_ADMIN;

Exercise

Try creating two constants. One constant should be created with the const keyword, and the other constant should be created with the define() function.

Key takeaways

  • Constants are variables whose values can't change once created.
  • You can create a constant with the const keyword followed by the name and value of the constant.
  • An alternative syntax for creating constants is the define() function. It accepts the name and value of the constant.
  • It's common practice to use all uppercase letters and underscore characters to separate words in a constant name.

Comments

Please read this before commenting