In-depth analysis of PHP variable usage _ PHP Tutorial

Source: Internet
Author: User
Analyze the usage of PHP variables in depth. We are using PHP variable 1. define the CONSTANT define (CONSTANT, Helloworld.); a CONSTANT can only contain scalar data (boolean, integer, float, and string ). When calling constants, we only need to use PHP variable 1. define the CONSTANT define ("CONSTANT", "Hello world .");

Constants can only contain scalar data (boolean, integer, float, and string ).
When calling a CONSTANT, you only need to obtain the CONSTANT value with a simple name, instead of adding 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.

PHP variable 2. common variable $ a = "hello ";

PHP variable 3. variable (using two dollar signs ($ ))

$ A = "world ";

Both variables are defined:

$ A's content is "hello" and $ hello's content is "world ".

Therefore, it can be expressed:

Echo "$ a $ {$ a}"; or echo "$ a $ hello"; both outputs: hello world
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.

PHP variable 4. static variable

In the function, static $ a = 0;
Note: assigning values to the results of expressions in the declaration results in parsing errors such as static $ a = 3 + 3; (error)
Static variables only exist in the local function domain (inside the function). after the function is executed, the variable values are not lost and can be used for recursive calls.

PHP variable 5. global variable

Global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body, you can use a special PHP custom $ GLOBALS array to access variables globally:
For example: $ GLOBALS ["B"] = $ GLOBALS ["a"] + $ GLOBALS ["B"];
A real global variable imported using a global statement in a function domain is actually a reference to a global variable.

Global $ obj;

Note: the static and global definitions of variables are implemented in the application mode.

PHP variable 6. assign a value to the variable: assign a value to the transfer address (simple reference ):

$ Bar = & $ foo; // add the & symbol to the variable to be assigned a value.

Changing new variables will affect the original variables. this assignment operation is faster.
Note: Only named variables can assign values to addresses.
Note: If

$ Bar = & $;
$ Bar = & $ foo;

Changing the value of $ bar can only change the value of the variable foo without changing the value of a (the reference has changed)

PHP variable 7.PHP Super global variable $ GLOBALS:

Contains a reference to a variable that is valid globally for each current script. The key of the array is the name of the global variable. $ GLOBALS array exists from 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 $ HTTP_SERVER_VARS array (still valid, but not used ).
$ _ GET: the variable submitted to the script through the http get method.
$ _ POST: the variable submitted to the script through the http post method.
$ _ COOKIE: variables submitted to the script through HTTP Cookies.
$ _ FILES: variables submitted to the script by uploading the http post file.
Enctype = "multipart/form-data" must be included in the file upload form"
$ _ ENV: the variable submitted to the script in the execution environment.
$ _ REQUEST: the variables submitted to the script through GET, POST, and COOKIE mechanisms. Therefore, this array is not trustworthy. All the existence or not of the variables contained in the array and the Order of the variables are defined according to the variables_order configuration instructions in php. ini. This array does not directly simulate earlier versions of PHP 4.1.0. See import_request_variables (). Note: Since PHP 4.3.0, the file information in $ _ FILES no longer exists in $ _ REQUEST.
$ _ SESSION: the PHP variable currently registered to the script SESSION.


Define PHP variable 1. define the CONSTANT define (CONSTANT, Hello world.); a CONSTANT can only contain scalar data (boolean, integer, float, and string ). When calling a constant, you only need simple...

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.