PHP, variables

Source: Internet
Author: User

1. Variable definition:

A variable is the amount of data that can change during the execution of a program. Variables are identified by a name (variable name). The system assigns a storage unit to each variable in the program, and the variable name is the name of the computer memory unit. Therefore, the variable name allows access to the in-memory data.

2. Variable declaration and definition:

Unlike many languages, you do not need to declare a variable before using it in PHP (you need to declare a variable before PHP4), just assign a value to the variable. Variable names in PHP are represented by $ and identity, and variable names are case-sensitive. Variable assignment refers to a specific data value for a variable, which can be assigned by "=" for variables of string and number types, and of course the variable-naming rules are followed for assigning values to variables. In addition to the direct assignment, there are two other ways, one is the assignment between variables, the other is the reference assignment, that is, to access the same variable content with different names, and when changing the value of one of the variables, the other changes as well, the reference assignment uses "&" "symbol to represent the reference.

3. Scope of variables:

Variables are used to conform to the defined rules of the variable. A variable must be used within a valid range, and the variable loses its meaning if the variable is outside the valid range. The scope of the variable is as follows:

Local variables: variables defined inside a function whose scope is the function in which the variable and the corresponding value are cleared when the function that declares the variable is exited;

Global variables: Variables that are defined outside of all functions, scoped to the entire PHP file, but are not available inside the user-defined function. If you want to use global variables inside user-defined functions, declare global variables using the global keyword;

Static variables: The ability to retain the value of a variable after the function call has ended, and when it returns to its scope again, it can continue to use the original value. The general variable is that after the function call ends, its stored data values are lifted, and the memory space occupied is freed. When using a static variable, you first declare the variable with the keyword static and put the keyword static before the variable you want to define.

4. Variable variable:

A mutable variable is a strange variable that allows you to dynamically change the name of a variable. The way it works is that the name of the variable is determined by the value of the other variable, and the implementation process is to add a dollar sign "$" to the front of the variable.

5.PHP pre-defined variables:

PHP also provides a number of useful predefined variables that can be used to obtain information such as the user's reply, the environment of the user's operating system, and the environment of the local operating system. The usual predefined variables are as follows:

Name Description of the variable

$_server[' server_addr ' The IP address of the server where the script is currently running

$_server[' server_name ') the name of the server host where the script is currently running. If the script is running on a virtual host, the name is determined by the value set by the virtual host.

$_server[' Request_method ' the request method when accessing the page. such as Get,head,post,put, if the request is in the way that the head,php script will abort after the output header information (which means that no output buffers are available after any output is produced)

$_server[' REMOTE_ADDR ' is browsing the current page user's IP address

$_server[' Remote_host ' is browsing the host name of the current page user. Reverse domain name resolution based on the user's remote_addr

$_server[' Remote_port ') the port that the user is using when connecting to the server

$_server[' script_filename ') the absolute pathname of the currently executing script. Note that if the script is executed in the CLI as a relative path, such as file.php or .../file.php,$_server[' script_filename ') will contain the user-specified relative path.

$_server[' Server_port ') the port used by the server, which defaults to 80, if SSL secure connection is used, this value is the HTTP port set by the user

s_server[' Server_signatrue ' contains a string of server version and virtual machine name

$_server[' Document_root ') the document root directory where the script is currently running. Defined in the server configuration file

$_cookie the information passed to the script via HttpCookie. Most of these cookies are set by the Setcookie () function when the PHP script is executed.

The $_session contains information about all the reply variables. The $_session variable is mainly applied to the pass-through control and the value between pages.

The $_post contains information about the parameters passed through the POST method. Used primarily to obtain data submitted via the Post method

The $_get contains information about the parameters passed by the GET method. Used primarily to obtain data submitted through the Get method

$GLOBALS An array that consists of all defined global variables. The variable name is the index of the array. It can be called as a super collection of all the super variables

PHP, variables

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.