Explanation of PHP variable principle

Source: Internet
Author: User
This article is mainly to share with you the PHP variable principle of explanation, hope to help you understand and master PHP variables.

First, the concept of variables

The so-called variable is the amount of the value that can change in the program.

Procedures are the management and processing of data. In the process of running the program, we need to store this data, variables and constants are used to save the program runtime data.

A variable is usually made up of two parts, a variable name and a variable value


1.1 in PHP, define a variable, using the $ symbol, regardless of the data type of the variable at the time of definition.

1.2 Variable assignment, modification, destruction

Increment is the assignment, such as $ A = "PHP"

Re-assign the value, $a = "Mysql"

By way of reference, such as $a

Delete, use unset such as unset ($a)

What the hell did 1.3 Unset do?

The variable reference is removed and the variable is destroyed.

Second, the definition of the specification of variable name

1. Variable names are usually composed of letters, numbers, and underscores, not beginning with a number.

2. See the meaning of the name

3. For a variable name with multiple words, how to divide the word between words, firstname,first_name. Romans do

Attention to detail

$ is not part of the variable name, which is the PHP variable syntax, meaning that the identifier behind it is a variable (special this variable)

When using a variable that is not defined, a notice error is reported, and you can use Isset to check

The variable name is case sensitive, and it is recommended to use the underscore method

Iii. Assignment between variables

In PHP, variable assignment defaults to the way value is passed, which is the basic way to assign PHP.

There is also a way to pass a value, which is a reference pass.

Attention to detail

unset A variable, delete the variable, and the reference between the identifier and the variable

A variable name exists in memory that satisfies the criteria for reference assignment. ($bar = & (24 * 7);//illegal;)

Four, variable variable

Variable name (variable identifier), which can also be a variable, which is a mutable variable.

A simple example

V. Pre-defined variables

There are many variables in PHP that can be used directly without the user's script definition, called predefined variables.

$_post

$_get

$_request

$_server

$_fiels

$_session

$_cookie

$_env

$GLOBALS

About Get and post, if there is a variable at the same time as Get and post, which value should be taken. (Agreed in the php.ini configuration)

function is the back post.

$_requst = $_post + $_get

If you can explicitly post or get commits, use post or get directly, and when not clear, then you can use $_request.

$_server, and some information about the HTTP protocol and the server.

Attention to detail

When using request, if both post and get have an identical variable name, only post is retained, depending on the configuration of PHP Request_order = ' GP ';

$GLOBALS referencing all variables available in the global scope

Vi. Scope of variables

What is a scope?

Refers to the scope of the variable to be effective.

The global variable (that is, the active range, in the current script, expires at the end of the script run.) )

Local variables

Super Global

Related recommendations:

PHP variable explanation and string dynamic Insert variable

In-depth understanding of PHP variable structure

Introduction to 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.