One, constant
A constant is an identifier (name) of a simple value. As its name implies, the value cannot be changed during script execution (except for the so-called Magic constants, which are not constants). Changshime think case sensitive. By convention constant identifiers are always capitalized.
The constant name and any other PHP tags follow the same naming convention. A valid constant name begins with a letter or an underscore followed by any letter, number, or underscore. This is expressed in a regular expression :
[a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]* (Note: Here, the letters are a-z,a-z, and ASCII characters from 127 to 255 (0X7F-0XFF))
As with super Globals, the range of constants is global. You can access constants anywhere in a script without having to control the scope. For more information, read the scope of variables in the manual.
Second, the grammar
You can use the Define () function to define constants. Once a constant is defined, it cannot be changed or canceled during the execution of the program.
Constants can only contain scalar data (boolean,integer,float and string).
You can simply get the value of the constant by specifying its name, and do not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to read the value of the constant. You can get a list of all the defined constants with Get_defined_constants ().
Note: Constants and (global) variables are in different namespaces. This means that, for example, TRUE and $TRUE are different.
If you use an undefined constant, PHP assumes that you want the name of the constant itself, as you would call it with a string (CONSTANT corresponds to "CONSTANT"). A e_notice level error is emitted at this time. See the Manual for Why $foo [bar] is wrong (unless you first define bar as a constant) with define (). If you only want to check whether a constant is defined, use the defined () function.
Three, constants and variables of the different
Constants are not preceded by a dollar sign ($);
Constants can only be defined with the Define () function, not by assignment statements;
Constants can be defined and accessed anywhere, regardless of the variable-scope rules;
Constants cannot be redefined or redefined once they are defined;
The value of a constant can only be a scalar.
Constants Definition Example:
<?php
define ("CONSTANT", "Hello world.");
Echo CONSTANT; Outputs "Hello world."
Echo Constant; Outputs "Constant" and issues a notice.
? >
Iv. Predefined constants
PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extensions, and are only available when the extensions are loaded, either dynamically or at compile time.
There are four magic constants that vary according to where they are used. For example, the value of __line__ depends on the line in which it is located in the script. These special constants are case-insensitive, as follows:
a few PHP "Magic constants"
The current line number in the __line__ file.
The full path and file name of the __file__ file.
__function__ function name (this is a new addition to PHP 4.3.0).
The name of the __class__ class (this is a new addition to the PHP 4.3.0).
The method name of the __method__ class (this is a new addition to the PHP 5.0.0).
The above is a detailed introduction of the basic content of PHP constants, I hope to help you learn.