5 Rules of PHP Constants

preview_player
Показать описание
Here are the five rules of PHP constants.

1. Constants must start with a letter or an underscore, followed by letters, numbers, and underscores.

2. It is a convention or common practice to declare a constant in uppercase letters, though it is not mandatory. It is helping developers to separate constants from variables.

3. A value must assign to a constant and this value cannot be changed at runtime.

4. In PHP, constants can be created by using either define() function or const keyword.

define function accepts two parameters, first one is the constant name and second one is the constant value.
define("GREET", "Good Morning");
echo GREET;

const keyword is used just before the constant declaration and assign a value to it.
const PI = 3.1416;
echo PI;

5. There are two difference between define and const.
1. define can be created in another block scope such as if statement or function, but const cannot.
2. define can contain any type of values, but const can contain only scalar values such as int, float, string, bool.
Рекомендации по теме