Here I am back again with a new topic of PHP. Constants! with its name you can guess, it’s something which does not change or can’t be changed. In PHP you can store a value in two ways. First is variable and another is constant. A value stored in a variable can be changed at any point, but once defined, the value of a constant can not be changed ever.
A constant can be used to store values which will not be going to changed throughout the program or application for the example database name, any application name or anything.
How to define Constant?
In PHP constants are defined using define() function. define() accepts 2 parameters first is the name and second is the value of the constant. The value of a constant can be used just by using it. Let us take an example.
<?php define( 'APP_NAME', 'PHP Tutorials!'); echo 'Hello World my website name is'.APP_NAME; //OUTPUT Hello World my website name is PHP Tutorials! ?>
Have you noticed something? the constant was used without $ (Dollar) symbols. Yes, you are right here. You don’t need to do use $ to use it or declare it.
Best way to declare Constant
The constant name follows the same rules as the variable name does. But a constant defined without $. A good programmer always defines a constant name in all uppercase.