Php variable definition method _ php instance

Source: Internet
Author: User
Some common definitions of static constants in php. 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.
2. common variable $ a = "hello ";
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.
4. static variables
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.
5. global variables
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.
6. assign a value to the variable: assign a value to the 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)
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 variable currently registered to the script SESSION.
How to disable phpinfo ():
Php. ini
Disable_functions = phpinfo ()
Restart the web server.
Constants in php
A constant can only use define (constant name, constant value );
Constants can only contain scalar data (boolean, integer, float, and string ).
You can simply get the value of a constant by specifying its name. do not add the $ symbol before the constant. If the constant name is dynamic, you can also use the function
To read the value of a constant. Use get_defined_constants () to obtain a list of all defined constants.
Note: constants and (global) variables are in different namespaces. This means that for example, TRUE and $ TRUE are different.
If an undefined CONSTANT is used, PHP assumes that it wants the name of the CONSTANT, just like calling it with a string (CONSTANT corresponds to "CONSTANT "). At this time, an E_NOTICE-level error will be issued. See why $ w3sky [bar] is incorrect in the manual (unless bar is defined as a constant with define ). If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different:
* There is no dollar sign before a 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 variable range rules;
* Once defined, a constant cannot be redefined or canceled;
* The constant value can only be a scalar.
Define constants
Define ("CONSTANT", "Hello world .");
Echo CONSTANT; // outputs "Hello world ."
Echo Constant; // outputs "Constant" and issues a notice.
?>

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.