Directory
[1] constant definition [2] custom constant [3] system constant [4] constant value [5]defined ()
Constant definition
Constants are similar to variables, but constants cannot be changed or revoked once defined. The main function of constants is to avoid duplicate definitions, tamper with variable values, and improve code maintainability. Valid constant names begin with a character or underscore (there is no $ symbol in front of the constant name). In general, constants are all capitalized. Constants in PHP are divided into custom constants and system constants
[note] Unlike variables, constants are automatically global throughout the script
Custom Constants
A custom constant uses the Define () function, which uses three parameters
[1] First parameter defines the name of the constant
[2] The second parameter defines the value of a constant
[3] Optional Third parameter specifies whether the constant name is case sensitive, the default is False
The syntax format for the define () function is:
Define (string$constant _namemixed$value$case _sensitivetrue])
php$p = ' PI0 '; Define (' PI ', 3.14); Define (' PI ', 3.15); // invalid because the constants cannot be modified by definition Define ($p, 3.14); Echo PI; // 3.14 Echo "
"; Echo PI0; // 3.14?>
System constants
System constants are constants that PHP has already defined, and common system constants are:
[1]__file__:php program file name. Gets the current file's physical location on the server
[2]__line__:php number of program file lines. Indicates the current code in the first line
[3] Php_version: The version number of the current parser. Can know in advance whether PHP code can be parsed by the PHP parser
[4] Php_os: Executes the current PHP version of the operating system name. Tell the server which operating system name to use, optimize the code based on the operating system
PHPecho__file__; // D:\wamp\www\1.php Echo "
"; Echo __line__; // One Echo "
"; Echo php_version; // 5.5.12 Echo "
"; Echo Php_os; // WINNT Echo "
";? >
Constant value
There are two ways to get a constant value: one is to get the value directly using the constant name, and the other is to use the constant () function
The constant () function is syntactically formatted as: mixed constant (string constant_name)
PHP$p; Define ("PI1", 3.14); Define ("PI2", 3.142); $height = "Medium"; if ($height = = "Medium") {$p = "PI1";} Else { $p = "PI2";} $r = 1; $area constant $r $r ; Echo $area ;? >
Defined () function
If the constants are defined repeatedly, the PHP parser will issue a "Constant XXX already defined" warning that the constant has been defined
The defined () function is used to determine whether a constant has been defined and its syntax is:
defined (string constants_name)
Returns true if it exists, otherwise false
Define ("PI1", 3.14); $p = "PI1"; $is 1 defined ($p); $is 2 defined ("PI2"); Var_dump ($is 1); // bool (TRUE) Var_dump ($is 2); // bool (false)?>
The above describes the front-end learning PHP constants, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.