PHP 5 Constants
After constant value is defined elsewhere in the script not to be changed.
PHP constants
Constant is an identifier for a simple value. This value can not be changed in the script.
A constant consists of English letters, underscores, and numbers, but the numbers do not appear as the first letter. (Constant names do not need to add $ modifier).
Note: Constant throughout the script can be used.
Setting PHP constants
Setting constants using define () function, the function syntax is as follows:
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
This function takes three arguments:
- name: Required parameters, constant name, that flag.
- value: the value of mandatory parameters, constants.
- case_insensitive: optional parameter, if set to TRUE, the constant is case-insensitive. The default is case-sensitive.
The following example we create a case-sensitive constant, constant value of "Welcome to w3big.com":
<?php // 区分大小写的常量名 define("GREETING", "欢迎访问 w3big.com"); echo GREETING; // 输出 "欢迎访问 w3big.com" echo '<br>'; echo greeting; // 输出 "greeting" ?>
The following example we create a case-insensitive constant, constant value of "Welcome to w3big.com":
<?php // 不区分大小写的常量名 define("GREETING", "欢迎访问 w3big.com", true); echo greeting; // 输出 "欢迎访问 w3big.com" ?>
Constants are global
After defining the constants, the default is a global variable that can be used anywhere in the entire run of the script.
The following example demonstrates the use of constants within a function, even if constants are defined in the outer function can be used normally constant.
<?php define("GREETING", "欢迎访问 w3big.com"); function myTest() { echo GREETING; } myTest(); // 输出 "欢迎访问 w3big.com" ?>