Constant is used for data that is unchanged at multiple places within our program. In this tutorial, we will learn how to create constants.
Constants in PHP:
Constants are an identifier or a name that can be assigned any fixed value.
The constants are similar to a variable except that they can never be changed. They remain constant throughout the program and cannot be altered during execution.
Variables are stored temporary but Constants are permanently stored.
Constants are always case-sensitive. And its name must start with a number.
It always starts with a letter or underscores, followed by letter, numbers or underscore. It should not contain any special characters except underscore, as mentioned.
Creating a PHP Constant:
By using define() function, you can create a constants which accept two arguments: The name of the constant and its value.
In the above example, the constants are defined as a key-value pair. The key is used to get the value of the constant.
In this example, the define() function gets only two parameters. The define() function has an three parameter but third is optional to set the case insensitivity of a constants. By default, the constants are case sensitive. We can make them case insensitive by setting the case insensitivity is true. So, the above example can be changed as,
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok