PHP constants and variables
[Constant]
You can use the define () function to define constants. After PHP 5.3.0, you can use the const keyword to define constants outside the class definition. Once a constant is defined, it cannot be changed or undefined.
Constants can only contain scalar data (boolean, integer, float, and string ). Resource constants can be defined, but should be avoided as far as possible, because unexpected results may occur.
You can simply specify its name to obtain the constant value. Unlike variables, you should not add the $ symbol before the constant. If the constant name is dynamic, you can use the constant () function to obtain the constant value. Use get_defined_constants () to obtain a list of all defined constants.
If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different as follows:
? There is no dollar sign before the constant ($ );
? Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
? Constants can be defined and accessed anywhere regardless of the scope of variables;
? Once defined, a constant cannot be redefined or canceled;
? The constant value can only be a scalar.
Predefined Constants
Many constants are defined by different extension libraries. They only appear when these extension libraries are loaded, dynamically loaded, or included during compilation. These special constants are case-insensitive, as follows:
Name |
Description |
__LINE__ |
The current row number in the file. |
__FILE__ |
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2,__FILE__ It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains a relative path. |
__DIR__ |
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalentDirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) = |
__FUNCTION__ |
Function Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters. |
__CLASS__ |
Class Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared region (for exampleFoo \ Bar). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method. |
__TRAIT__ |
Trait name (New in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for exampleFoo \ Bar). |
__METHOD__ |
Class Method Name (New PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ). |
__NAMESPACE__ |
Name of the current namespace (case sensitive ). This constant is defined during compilation (New in PHP 5.3.0 ). |
Variable]
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name must start with a letter or underline followed by any number of letters, numbers, or underscores.
By default, values are always assigned. That is to say, when the value of an expression is assigned to a variable, the value of the original expression is assigned to the target variable. This means that, for example, changing the value of one variable when the value of a variable is assigned to another variable does not affect another variable. PHP also provides another way to assign values to variables: assign values to references. This means that the new variable simply references the original variable (in other words, "become its alias" or "point. Changing new variables will affect the original variables, and vice versa. Assign values by reference.
&Add the symbol to the variable to be assigned a value (source variable ).
Predefined Variables
In PHP 4.2.0 and later versions, the default value of the PHP Command register_globals is off. This is a major change in PHP. Setting the value of register_globals to off affects the validity of the predefined variable set within the global range. For example, to obtain the DOCUMENT_ROOT value, you must use $ _ SERVER ['document _ root'] instead of $ DOCUMENT_ROOT, as shown in the following figure, use $ _ GET ['id'] instead of $ id from URL http://www.example.com/test.php? In id = 3, get the id value, or use $ _ ENV ['home'] instead of $ HOME to get the value of the environment variable HOME.
? Hyper-global variables are built-in variables that are always available in all scopes.
? $ GLOBALS-reference all available variables in the global scope
? $ _ SERVER-SERVER and execution environment information
? $ _ GET-http get variable
? $ _ POST-http post variable
? $ _ FILES-variables for uploading HTTP FILES
? $ _ REQUEST-HTTP Request variable
? $ _ SESSION-Session variable
? $ _ ENV-Environment Variable
? $ _ COOKIE-HTTP Cookies
? $ Php_errormsg-previous error message
? $ HTTP_RAW_POST_DATA-native POST data
? $ Http_response_header-HTTP Response Header
? $ Argc-number of parameters passed to the script
? $ Argv-parameter array passed to the script
Global keyword
The global variables in PHP are a little different from those in C language. In C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. Some may accidentally change a global variable. Global variables in PHP must be declared as global or custom $ GLOBALS arrays in PHP. $ GLOBALS is an associated array. Each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable.
Static variables
Another important feature of variable range is static variable ).
Variable
Sometimes it is convenient to use a variable name. That is to say, the variable name of a variable can be dynamically set and used. A common variable is set through declaration.
To use variables in an array, you must solve an ambiguous problem. When writing $ a [1], the parser needs to know that $ a [1] is used as a variable, you still want $ a as a variable and retrieve the value of [1] In the variable. The syntax for solving this problem is to use $ {$ a [1]} For the first case and $ {$ a} [1] For the second case.
"; Echo invalide_value. ""; if (defined ("INVALIDE_VALUE") {echo "INVALIDE_VALUE has been defined ". _ LINE __. "";} // variable $ str1 = 'str1'; $ str2 = & $ str1; // reference $ str1 = "Changed $ str1"; echo $ str1 .""; echo $ str2. ""; echo $ _ SERVER ['document _ root']. ""; // predefined variable $ gVal = 13; function Test () // global variable {global $ gVal; echo $ gVal. ""; echo $ GLOBALS ['gval ']. "" ;}test (); function test1 () {static $ a = 0; // static variable echo $ a; $ a ++;} // variable class foo {Var $ bar = 'I am bar. '; var $ arr = array (' I am. ',' I am B. ',' I am C. '); var $ r =' I am r. ';}$ foo = new foo (); $ bar = 'bar'; $ baz = array ('foo', 'bar', 'baz', 'quux '); echo $ foo-> $ bar. ""; echo $ foo-> $ baz [1]. ""; $ start = 'B'; $ end = 'ar '; echo $ foo-> {$ start. $ end }. ""; $ arr = 'arr'; echo $ foo-> $ arr [1]. ""; echo $ foo-> {$ arr} [1]. "";?>