Definitions of variables and variables in PHP

Source: Internet
Author: User
Tags constant file upload http post numeric value uppercase letter

Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can reuse it in the script.

All variables in PHP start with the $ symbol.

The correct method for setting variables in PHP is:

$ Var_name = value; PHP users often forget the $ symbol before the variable. In this case, the variable will be invalid.

Let's try to create a variable with a string and a variable with a numeric value:

Instance 1

The code is as follows: Copy code

<? Php
$ Txt = "Hello World! ";
$ Number = 16;
?>

Instance 2

The code is as follows: Copy code

<? Php
$ Foo = 25;
$ Bar = & $ foo; // valid value assignment
$ Bar = & (24*7); // invalid; reference an expression without a name
Function test ()
{
Return 25;
}

$ Bar = & test (); // invalid
?>

Predefined variables
PHP provides a large number of predefined variables. Because many variables depend on the version and settings of the running server, and other factors, there is no detailed description document. Some predefined variables do not take effect when PHP is run as a command line.


Variable range
The scope of a variable is the context scope defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require.

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. The local variables in PHP are not overwritten by global variables. If used, they are the default initial values of the variables. This may cause some problems. Some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.

Use static variables
Another important feature of variable range is static variable ). Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope.


Note:
Variable naming rules
The variable name must start with a letter or underscore.
Variable names can only contain letters, numbers, and underscores.
Variable names cannot contain spaces. If the variable name is composed of multiple words, it should be separated by underscores (for example, $ my_string) or begin with an uppercase letter (for example, $ myString ).


Php variable definition

1. Define constantsDefine ("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 variables$ A = "hello ";
3. Variable(Use 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 values to variables: 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)
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.

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.