php constants
PHP 5 Constants
After a constant value is defined, it cannot be changed anywhere else in the script.
PHP Constants
A constant is an identifier of a simple value. This value cannot be changed in the script.
A constant consists of English letters, underscores, and numbers, but numbers cannot appear as the first letter. (The $ modifier is not required on the constant name).
Note: Constants can be used throughout the script.
Set PHP constants
To set constants, use the define() function. The function syntax is as follows:
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
This function has three parameters:
· name: required parameter, constant name , that is, the identifier.
· value: required parameter, the value of a constant.
· case_insensitive: Optional parameter, if set to TRUE, the constant is case-insensitive. The default is case-sensitive.
In the following example we create a case-insensitive constant, the constant value is "Welcome to php.cn":
<?php // 区分大小写的常量名 define("GREETING", "欢迎访问 php.cn"); echo GREETING; // 输出 "欢迎访问 php.cn" echo '<br>'; echo greeting; // 输出 "greeting" ?>
In the following example we create a case-insensitive constant, the constant value is "Welcome to php.cn":
<?php // 不区分大小写的常量名 define("GREETING", "欢迎访问 php.cn", true); echo greeting; // 输出 "欢迎访问 php.cn" ?>
Constants are global
After a constant is defined, it defaults to a global variable and can be used anywhere in the entire running script.
The following example demonstrates the use of constants within a function. Even if the constant is defined outside the function, the constant can be used normally.
<?php define("GREETING", "欢迎访问 php.cn"); function myTest() { echo GREETING; } myTest(); // 输出 "欢迎访问 php.cn" ?>
Constant: Once declared, the value of this constant will not be changed.
1, the constant is declared using the function define()
2 If the constant is not declared, the constant name will be automatically converted into a string when used
3. Constant name without $ sign.
4. Constant names are case-sensitive by default, one two ONE TWO. It is customary for constant names to be all capitalized.
5. Use the third parameter of define() to decide whether to make this constant name case-sensitive true false For example
define("ROOT",10,true);
6. Constants only support scalar data types
7. Constants cannot use unset() to clear a constant
8. You can use defined() to determine whether a constant exists
For example
<?php define("ROOT",10,ture); if(defined("ROOT")){ echo ROOT+10; }else{ echo"11111111111"; } ?>
9. You can use the function constant() to read the value of a constant.
10. Use get_defind_constants() to get a list of all defined constants