Add to Favorites

php constants

Let's say you want to define a variable that cannot be changed after it is defined. You'd like to use this variable for a configuration option, message, or something else of this sort. You can do this using constants. Constants are a programming structure common in many programming languages. It is a scalar variable that cannot be changed once defined. In addition to not being changed, it is in the global scope automatically.

Here is how you would define a php constant

 
define('MY_CONSTANT',3);
 

It is common to define a constant name in all caps, this makes it easier to identify it's use as a constant. You can use this constant in your programming pretty easily

 
echo 45 * MY_CONSTANT;
 

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up