1:55 AM

Constant

posted under by FR3@K | Edit This
Constants are like variables but their values can't be changed after they are set. Constants are defined like that:


define( 'MY_CONSTANT' , 298 );

this statement defines a constant named as MY_CONSTANT . you don't need to write the constant names with all capital letters but i can say that this is common (or traditional) for defining constants in some other languages too.
while using a constant in the rest of your code you just write its name anywhere that you want its value to be.

echo "my constant is: ";
echo MY_CONSTANT;

you see that we put no $ sign before the constant name. that's because a constant is not a variable. constants will be very useful when you have a constant value in your code that is used many times in the code and you frequently change it. by using a constant you don't need to change every statement that has the value you want to change. you only change the definition of the constant. you may also want to use constants when you want to keep some special values that you don't want to memorize or write everytime you need it (pi, speed of light, gravity constant etc.) .

0 comments

Make A Comment
top