Home > Article > Backend Development > Are php constants global?
PHP Constants
Constant is an identifier (name) of a single value. This value cannot be changed in script.
Valid constant names begin with a character or an underscore (there is no $ sign in front of the constant name). (Recommended learning: PHP video tutorial)
Note: Unlike variables, constants are automatically global throughout the entire script.
Set PHP constants
To set constants, use the define() function - it uses three parameters:
The first parameter defines the constant The name
The second parameter defines the value of the constant
The optional third parameter specifies whether the constant name is case-insensitive. The default is false.
The following example creates a case-sensitive constant with the value "Welcome to php.cn!":
Example
<?php define("GREETING", "Welcome to php.cn!"); echo GREETING; ?>
Constants are global
Constants are automatically global and can be used throughout the entire script.
The following example uses a constant inside a function even though it is defined outside the function:
Example
<?php define("GREETING", "Welcome to php.cn!"); function myTest() { echo GREETING; } myTest(); ?>
The above is the detailed content of Are php constants global?. For more information, please follow other related articles on the PHP Chinese website!