Global PHP Constants | PHP Gurukul
What is PHP Constants?
Constants are variables
Constants are variables except that once they are defined they can’t be changed or undefined.
- A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
- A valid constant name starts with a letter or underscore.
- (no $ sign before the constant name).
- Note: Unlike variables, constants are automatically global across the entire script.
How to create a PHP constant
- You can create a constant by using the define () function.
- Use the define function to specify the name and value of a constant. (name, value, case-insensitive).
- Many programmers use capital letters to name constants – so they stand out.
- Note: Since a constant can not be change you do not use a $ sign before it’s name when you declare it or (use it).
Parameters
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
How to declare constants
Examples:
1 2 | define(‘male’, ‘m’) ; // a string constant define(‘PI’, 3.14159) ; // a double constant |
PHP Constant Examples
1 2 3 4 5 6 7 8 | <!DOCTYPE html> <html> <body> <?php// case-sensitive constant name define("GREETING", "Welcome to PHPGURUKUL.com!"); echo GREETING;?> </body> </html> |
Output: Welcome to PHPGURUKUL.com!
Click : https://phpgurukul.com/php-constant/
Difference Between Constant and Variables
- The mentioning of a dollar sign($) is the first evident difference between constants and variables. Variables have to be preceded by a dollar sign while constants are specified without it.
- Variables are assigned values using the simple assignment. However, constants cannot be defined in this manner. Constants can only be defined using define() function.
- Variables can be assigned, reassigned and un-defined. How-ever, constant cannot be modified in any way during the course of program.
- The Variable scoping rules do not apply to contants. They can be accessed and defined anywhere
Magic Constants
There are predefined constants supported by PHP. Any script that runs on PHP can access these constants. Some constants in PHP change on the basis of where they are being used. For instance, the constant ___LINE___ is dependent on the line that is being used. These are case-insensitive. Some of the PHP constants, where are defined as ‘magical’ are as follows-
Magic Constant | Meaning |
---|---|
___LINE___ | This constant indicates the present line number. |
___FILE___ | This constant contains the path and file name of the file concerned. It is important to mention here that the path contained by this constant is the absolute path. |
___FUNCTION___ | This constant includes the name of the function as it was when the function was declared. The name is case-sensitive |
___CLASS___ | This constant returns the name of the class concerned in a case-sensitive manner and exactly in the manner that it was declared. |
___METHOD___ | This constant returns the name of the method concerned in a case-sensitive manner and exactly in the manner that it was declared. |
Comments
Post a Comment