Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes PHP Constants

Natalie dw
Natalie dw
1,153 Points

Why would you use a CONSTANT rather than a VARIABLE?

After watching this video, it looks like CONSTANT's & VARIABLE's essentially work the same. So what's to stop you just using variables, instead of constants? Is there a set of rules for when you should one or the other?

3 Answers

Tomasz Budny
Tomasz Budny
14,588 Points

Well they do not exactly work the same. The key difference is that "value cannot change during the execution of the script (except for magic constants, which aren't actually constants)" - according to php.net.

Why do I use them? Code clarity is the first thing that comes to mind. Where ever I see a constant I can be sure that its value was not changed anywhere along the way since it being defined.

hey ! you simply use constants, with variables that can never change, like p(sign) =3.14, or a known math variable. its good for you to have a better organisation of your code !

to give another example the year you were born is a constant, while your postcode is a variable