PHP Constants

From Techotopia
Revision as of 13:45, 29 May 2007 by Neil (Talk | contribs) (New page: If you look up the word constant in a dictionary it will probably tell you that it refers to something that is non-changing and non-vary, and this is exactly the purpose of constants in PH...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

If you look up the word constant in a dictionary it will probably tell you that it refers to something that is non-changing and non-vary, and this is exactly the purpose of constants in PHP. A PHP constatnt is the opposite of a variable in that once it has been defined it cannot be changed.

Constants a reparticularly useful for defining a value that you frequently need to refer to that does not ever change. For example you might define a constant called H2O_BOILING_POINT that contains the boiling point of water. Since this is a value that typically doesn't change it makes sense to define it as a constant. Conversly a value that is likely to change, such as the Dollar to Yen exchange rate is best defined as a variable.

PHP constants are said to have global scope. This basically means that once you have defined a constant it is accessible from any function or object in your script.

Defining a PHP Constant