| Start |
|---|
| Defining constants Constants are created using the Define constant using explicit valuesDefine constant using another constantif you have one constant you can define another one based on it: Reserved constantsSome constant names are reserved by PHP and cannot be redefined. All these examples will fail: And a Notice will be issued: Conditional definesIf you have several files where you may define the same variable (for example, your main config then your local config) then following syntax may help avoiding conflicts: |
| Class Constants Constants can be defined inside classes using a This is useful to store types of items. |
Checking if constant is defined Simple checkTo check if constant is defined use the Note that constant becomes "visible" in your code only after the line where you have defined it: Getting all defined constantsTo get all defined constants including those created by PHP use the To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process): It's sometimes useful for debugging |
| Constant arrays Arrays can be used as plain constants and class constants from version PHP 5.6 onwards: Class constant examplePlain constant exampleAlso from version PHP 7.0 this functionality was ported to the |
| Using constants To use the constant simply use its name: or if you don't know the name of the constant in advance, use the Syntax
RemarksConstants (opens new window) are used to store the values that are not supposed to be changed later. They also are often used to store the configuration parameters especially those which define the environment (dev/production). Constants have types like variables but not all types can be used to initialize a constant. Objects and resources cannot be used as values for constants at all. Arrays can be used as constants starting from PHP 5.6 Some constant names are reserved by PHP. These include Constants are usually named using uppercase letters. |
Read Article
https://develop.horje.com/learn/1434/reference