PHP Constants and Variables _php tutorial

Source: Internet
Author: User
Tags http file upload scalar

PHP Constants and variables


"Constant"
Constants can be defined with the Define () function, and after PHP 5.3.0, constants can be defined outside the class definition using the Const keyword. Once a constant is defined, it can no longer be changed or undefined.
Constants can only contain scalar data (boolean,integer,float and string). You can define resource constants, but avoid them as much as possible, because they cause unpredictable results.
You can simply get the value of a constant by specifying its name, and unlike a variable, you should not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to get the value of the constant. A list of all defined constants can be obtained with get_defined_constants ().
If you only want to check if a constant is defined, use the defined () function.
Constants and variables have the following differences:
? The constant is preceded by a dollar sign ($);
? Constants can only be defined with the Define () function, not through assignment statements;
? Constants can be defined and accessed anywhere, regardless of the scope of the variable;
? Once a constant is defined, it cannot be redefined or undefined;
? The value of a constant can only be scalar.
Pre-defined constants

Many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time. These special constants are case-insensitive, as follows:

name Description
__LINE__ The current line number in the file.
__FILE__ The full path and file name of the file. If used in the included file, returns the file name that is included. Since PHP 4.0.2, __FILE__ always contains an absolute path (if the symbolic connection is the resolved absolute path), and the previous version sometimes contains a relative path.
__DIR__ The directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to dirname (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) =
__FUNCTION__ The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
__CLASS__ The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.
__TRAIT__ Trait's name (PHP 5.4.0 new Plus). From PHP 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The Trait name includes its declared function area (for example, Foo\bar).
__METHOD__ The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).
"Variables"
Variables in PHP are represented by a dollar sign followed by a variable name. Variable names are case-sensitive. The variable name follows the same rules as other tags in PHP. A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores.
Variables are always assigned values by default. That is, when you assign the value of an expression to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, when the value of one variable is assigned to another variable, changing the value of one of the variables will not affect the other variable. PHP also provides another way to assign a value to a variable: reference assignment. This means that the new variable is a simple reference (in other words, "becomes its alias" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa. Using reference assignment, simply add a &The symbol is added before the variable that will be assigned (source variable).
Pre-defined variables
PHP 4.2.0 and subsequent versions, the default value of PHP directive register_globals is off. This is a major change in PHP. Letting the register_globals value off will affect the validity of the set of predefined variables at global scope. For example, in order to get the value of Document_root, you would have to use $_server[' document_root '] instead of the $DOCUMENT _root, as in the case of the $_get[' ID ') instead of the $id from the URL http://www.exa Get the ID value in mple.com/test.php?id=3, or use $_env[' home ' instead of $HOME get the value of the environment variable HOME.
? Hyper global variable-a hyper-global variable is a built-in variable that is always available in all scopes
? $GLOBALS-referencing all variables available in the global scope
? $_server-server and execution Environment information
? $_get-http GET Variable
? $_post-http POST Variables
. $_files-http File Upload variables
? $_request-http REQUEST Variable
? $_session-session variables
? $_env-Environment Variables
? $_cookie-http Cookies
? $php _errormsg-Previous error message
? $HTTP _raw_post_data-Native POST data
? $http _response_header-http Response Head
? $ARGC-The number of arguments passed to the script
? $argv-An array of arguments passed to the script

Global keyword
PHP has a slightly different global variable from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may accidentally change a global variable. A global variable in PHP must be declared as global when used in a function or a custom $GLOBALS array with a special PHP. $GLOBALS is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable.
static variables
Another important feature of the variable range is the static variable (variable).
Variable variable
Sometimes it is convenient to use variable variable names. That is, the variable name of a variable can be set and used dynamically. An ordinary variable is set by a declaration.
To use mutable variables with arrays, you must address an ambiguous question. This is when you write the $ $a [1], the parser needs to know if you want to $a [1] as a variable, or you want $ $a as a variable and take out the value of the variable indexed to [1]. 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 already defined". __line__. ";;} Variable $str1 = ' str1 '; $str 2 = & $str 1;  Reference $str 1 = "Changed $str 1"; Echo $str 1. ""; echo $str 2. ""; 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 A. ', ' 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. "; $baz echo $foo [1]  . "; $start = ' B '; $end   = ' ar '; echo $foo->{$start. $end}  . ""; $arr = ' arr '; echo $foo $arr [1]. ""; echo $foo->{$arr}[1]  . ";? >








http://www.bkjia.com/PHPjc/1022400.html www.bkjia.com true http://www.bkjia.com/PHPjc/1022400.html techarticle PHP Constants and variable "constants" can use the Define () function to define constants, and after PHP 5.3.0, you can use the Const keyword to define constants outside of the class definition. Once a constant is determined ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.