PHP constants
PHP 5 Constants
After a constant value is defined, it cannot be changed anywhere else in the script.
PHP Constants
As its name suggests, the value of a constant is It cannot be changed. Constants are also case-sensitive. The naming rules for variables are the same. Legal constant names start with letters or underscores, followed by any letters, numbers or underscores.
In order to distinguish, constants are not added For the $ symbol, we usually agree that constants use capital letters
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, i.e. 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" ?>
Constant is global
Constant after definition , which is a global variable by default 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" ?>
System constants
In addition to system variables, PHP also has system constants. We often call system constants predefined constants. We can use it directly. Most of the system constants in PHP start and end with double underscores, with capital letters
• __LINE__ The line number of the current code
• __FILE__ The current code The file name where it is located
• __FUNCTION__ The function name where the current code is located
• __CLASS__, __METHOD__......
• PHP_VERSION The current PHP version number
• PHP_OS The operating system of the current server