PHP Learning Variable Usage Summary

Source: Internet
Author: User
Tags array constant file upload http cookie http post variables variable valid
1. Defining ConstantsDefine ("CONSTANT", "Hello world.");
Constants can only contain scalar data (boolean,integer,float and string).
When calling a constant, simply use the name to get the value of the constant, not the "$" symbol, such as: Echo CONSTANT;
Note: Constants and (global) variables are in different namespaces. This means that, for example, TRUE and $TRUE are different.

2. General variable$a = "Hello";

3. Variable variable (use two dollar sign ($))
$ $a = "world";
All two variables are defined:
$a content is "hello" and $hello content is "world".
Therefore, it can be expressed as:
echo "$a ${$a}"; or echo "$a $hello"; they all output: Hello World
To use variable variables with arrays, you must resolve an ambiguous problem. This is when you write $ $a [1], the parser needs to know if you want to $a [1] as a variable, or if you want $ $a as a variable and take out the value indexed by [1] in the variable. The syntax for solving this problem is to use the ${$a [1]} for the first case, with ${$a}[1] for the second case.

4. Static Variables
Static $a = 0 within the function;
Note: Assigning a value to an expression in a declaration can result in parsing errors such as static $a =3+3; (error)
A static variable exists only in a local function field (within a function), the value of the variable is not lost when the function is finished, and can be used to recursively call

5. Global Variables
Global variables defined in a function body can be used outside of the function body, and global variables defined in the function body cannot be used in the function body, and access to variables globally can be in a special PHP custom $GLOBALS array:
For example: $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];
A real global variable imported in a function field with the global statement actually establishes a reference to a global variable
Global $obj;
Note: The static and global definitions of variables are implemented in an applied manner

6. Assign value to variable: Transfer address Assignment (simple reference):
$bar = & $foo; Add & sign to the variable that will be assigned the value
Changing the new variable will affect the original variable, and this assignment is faster
Note: Only named variables can pass address assignment
Note: if
$bar = & $a;
$bar = & $foo;
Changing the value of $bar can only change the value of variable foo without changing the value of a (reference changed)

7.PHP Super global variable$GLOBALS: Contains a reference to a variable in the global scope that is valid for each current script. The key of the array is the name of the global variable. $GLOBALS array is present starting with PHP 3.
$_server: Variables are set by the WEB server or directly associated with the execution environment of the current script. Similar to the old array $HTTP _server_vars array (still valid, but opposed to use).
$_get: A variable that is submitted to a script via an HTTP get method.
$_post: A variable submitted to a script via the HTTP POST method.
$_cookie: A variable that is submitted to a script via an HTTP cookie method.
$_files: A variable submitted to a script via an HTTP POST file upload.
File Upload form should have enctype= "Multipart/form-data"
$_ENV: Executes the variables that the environment submits to the script.
$_request: A variable submitted to a script via the get,post and COOKIE mechanisms, so the array is not trustworthy. The existence of all variables contained in the array and the order of the variables are defined according to the Variables_order configuration instructions in php.ini. The array does not directly simulate an earlier version of the PHP 4.1.0. See Import_request_variables ().
Note: from PHP 4.3.0, the file information in $_files no longer exists in the $_request.
$_session: The variable currently registered to the script session.

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.