Defining constants
A constant is an identifier for a simple value. As the name implies, when a constant is defined during the execution of a script, it cannot be changed or de-defined. Changshime is considered to be case sensitive. By convention constant identifiers are always uppercase
The constant name and any other PHP tags follow the same naming conventions. A valid constant name begins with a letter or underscore, followed by any letter, number, or underscore.
As with hyper-global variables, the range of constants is global. The constant can be accessed anywhere in the script without a tube-action area
Constants can only contain scalar data (Boolean, Integer, float, and string). You can define resource constants, but try to avoid them because they cause unpredictable results.
Define () function
Defining constants uses the Define () function, which uses three parameters: the first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name is case-sensitive and the default is False
BOOL Define (string name, mixed value [, BOOL case_insensitive])
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
<?php$p = ' PI0 ';d efine (' pi ', 3.14);d efine (' pi ', 3.15);//Invalid because constants cannot be modified by definition define ($p, 3.14); Echo Pi;//3.14echo "<br > "; Echo pi0;//3.14?>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
Const
After PHP5.3.0, you can use the Const keyword to define constants outside of the class definition
Defining constants using the Const keyword must be at the top of the scope, because this method is defined at compile time. This means that constants cannot be defined within a function, within a loop, or within an if statement with a const
<?php//The following code will work correctly after PHP 5.3.0 const CONSTANT = ' Hello world '; Echo CONSTANT; >
Constant detection
For constants, it is common to detect whether a constant defines or detects the value of a constant, involving the defined () function and the constant () function
Defined () function
The defined () function is used to determine whether a constant has been defined and its syntax is:
bool Defined (string constants_name)
Returns true if it exists, otherwise false
If the constants are defined repeatedly, the PHP parser will issue a "Constant XXX already defined" warning that the constant has been defined
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
<?php define ("PI1", 3.14); $p = "PI1"; $is 1 = defined ($p); $is 2 = defined ("PI2"); Var_dump ($is 1);//bool (True) var_dump ($ IS2);//bool (false)?>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
Constant () function
The constant () function is used to return a constant value in the syntax format:
Mixed constant (string constant_name)
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
<?php$p;define ("PI1", 3.14);d efine ("PI2", 3.142), $height = "Medium", if ($height = = "Medium") {$p = "PI1";} else{$p = "PI2";} $r = 1; $area = constant ("PI") * $r * $R; echo $area;? >
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
System constants
In PHP, in addition to defining constants on their own, a series of system constants are pre-defined and can be used directly in the program to accomplish some special functions. Here are some of the predefined constants that are common in the system
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
such as Php_os unix or Winnt Perform PHP parsing of operating system name php_version 5.2.6, etc. Current PHP Version number e_error 1 error, cause PHP script to run stop e_warning 2 warning, does not cause PHP script to run stop E_ parse 4 parsing error, CAP program Parser report e_notice 8 non-critical errors, such as variables not initialized m_pi 3.1415926535898 PI values in mathematics
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
Complete list of system constants
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
<?; "<br/>"; "<br/>"?>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
Magic Constants
There are 8 system constants in PHP that change depending on where they are used, such constants are called Magic constants
The current line number in the __line__ file. The full path and file name of the __file__ file. If used in the included file, returns the file name that is included. From php 4.0.2 onwards,__file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path __dir__ The directory where the files are located. If used in the included file, returns the directory where the included files are located. Unless it is a root directory, the name in the directory does not include the trailing slash. (php 5.3.0 new) __function__ function name (php 4.3.0). Since PHP5 this constant returns the name (case-sensitive) when the function is defined. In PHP4, the value is always lowercase letters. The name of the __class__ class (php 4.3.0 new addition). Since PHP5 this constant returns the name (case-sensitive) when the class is defined. In PHP4, the value is always lowercase letters. The class name includes the name of its declared action area (for example, foo\bar) __trait__ trait (PHP 5.4.0 (new addition). From php 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The trait name includes the method name of the __method__ class for its declared action area (for example, foo\bar) (php 5.0.0 new). Returns the name (case-sensitive) of the __namespace__ current namespace (case-sensitive) when the method is defined. This constant is defined at compile time (php 5.3.0 new)
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
<?phpecho __file__;//d:\wamp\www\1.phpecho "<br/>"; Echo __line__;//11?>
Constants of PHP