PHP Constants Tutorial
In this tutorial we will learn about immutable data containers in PHP, called constants, that don't allow their values to be changed when the application runs.
What is a constant?
A constant is a data container similar to a variable except that once a constant is initialized, its value cannot be changed at runtime.
We use constants when we have data that should not be able to change, even accidentally, at runtime. An example of this would be the value of pi, or gravity.
How to declare a constant with const
Similar to other languages such as C#, we can use the const language construct to declare a constant.
We simply write the keyword const before the name of the constant.
const name = value;
<?php const PI = 3.14; echo PI; ?>
We won’t be able to change the value of “PI” at runtime.
<?php const PI = 3.14; echo PI; // will raise error const PI = 0; ?>
When we run example above, where we try to change the value of “PI” by assigning the value 0 to it, the interpreter will raise an error.
Notice: Constant PI already defined in C:\xampp\htdocs\PHPProjects\HelloWorld.php on line 8
How to declare a constant with define()
Another way to declare constants, and constant arrays, is by calling the define() function.
When calling a function we write the values for its parameters in between its parentheses, separating them with a comma.
define(name, value, case_insensitive)
The define() function has two mandatory parameters and a third, optional, boolean parameter.
- name: Specify the name of the constant in between quotes.
- value: Specify the value of the constant (i.e the data stored inside of it)
- case-insensitive (optional): Specify whether the constant name should be case-insensitive or not. The default value is false to indicate the name will be case-sensitive.
<?php define("PI", 3.14); echo PI ?>
In the example above, we define a constant with the name “PI” and the value of 3.14. Because we want the name to be case-sensitive we don’t specify the third parameter.
If we wanted the name to be case-insensitive, we would choose the boolean true for the third parameter.
<?php define("PI", 3.14, true); echo pi ?>
Because we specified true for the name to be case-insensitive in the example above, when we echo the constant with lowercase letters, the interpreter doesn’t raise an error.
declare() vs const. When to use which
The most important difference between the two is that const defines a constant at compile time, whereas define() defines a constant at run time.
Let's consider what happens when PHP runs a file. By default PHP makes two passes when it runs a file.
1. Pass one will parse the file and build the machine code. This is the raw binary code that your system will run, the sequences of 1's and 0's.
2. Pass two will execute the code from pass one, which is commonly referred to as runtime. At this point the computer is actually executing the instructions we programmed.
Because const defines a constant at compile time, it will cause some difficulties:
- We can't use it to conditionally define constants.
- We can only use it to define scalar constants (integers, floats, booleans and strings).
And, in any version prior to PHP v5.6:
- Expressions are not allowed in constant definition
- const constants cannot be an array of constants
It's not all bad though:
- const defines a constant in the current namespace, the define() function has to be passed the full namespace name even if we're working in the current namespace.
- Because the definition happens at compile time, they are quite a bit faster than define(), approximately twice as fast. This is especially practical when working with a large number of constants.
- const can be used within classes or interfaces to define class or interface specific constants, define() cannot be used this way.
- It reads much easier and programmers coming from other languages, like C#, will be much more comfortable with this style of constant definition.
- In any version before PHP v7, define() cannot be used to define arrays of constants.
When to use which
It’s up to your personal preference and the code you are currently working on. That said, from personal experience we advise the following.
- If you are working with a PHP version between v5.6 and 7, and you need to define arrays of constants.
- If you are defining scalar constants (integers, floats, booleans or strings).
- If you are working with a large number of constants.
- If you need class or interface specific constants.
- If you don’t want to specify the current namespace.
- If you find that it reads easier to you personally, or you are more comfortable with it.
- If you don’t need to check a constant in a conditional statement.
Other than the situations above, use define() for constant definition.
We haven’t covered many of the topics we spoke about in this section of the course yet. Once we do cover these topics, it will become clearer.
Summary: Points to remember
- The value of a constant cannot be changed at runtime.
- Constants can be defined with the const keyword or the define() function.