PHP variable definition and usage example

Source: Internet
Author: User
A constant refers to the amount of changes that can be made to values during program execution. it is opposite to the constant we are talking about. if a constant is defined, it cannot be changed in the program. Variables can be changed. What is a variable?

A variable refers to the amount of data that can change during program execution. it is opposite to the constant we are talking about. if a constant is defined, it cannot be changed in the program. Variables can be changed.

What are variables used?

A variable is used to save our values. it is equivalent to a container that stores data, for example

X = 5;

Y = 6;

Z = x + y,

From the above expression z = x + y, we can calculate that the value of z is 11.

In PHP, these three letters are called variables.

Variable assignment

Variable assignment refers to the specific data value of a variable. for string and numeric variables, "=" can be used to assign values, as in the above example, x, y is the variable, so 5 and 6 are the variable values.

After learning about the functions and values of variables, let's take a look at how variables are defined.

Variable definition

All the variables in PHP start with "$ ".

As shown below

$a=5;$b=6

The above example defines and assigns values to variables.

The variable name is case-sensitive. the variable name can start with a letter or underline, followed by any letter, number, or underline. the name of a variable like the following is correct:

 

The following is an incorrect variable name:

 

The variable name cannot start with a number, and the variable name cannot start with a letter or a character other than an underscore. Therefore, it is incorrect to define the variable above.

Although PHP variable names can start with an underscore, we generally do not recommend this because variables starting with an underscore are usually included in the PHP System.

In addition to directly assigning values to variables, there are two ways to declare variables and assigning values. one isValue assignment between variablesWhat does it mean? See the following example.

Example

 

Code running result:

Instead of directly assigning values to the variable $ str2, we assign $ str1 to $ str2 ,. This is the value assignment between variables.

The other isReference value assignment.Starting with PHP4, PHP introduced the concept of "reference assignment", that is, accessing the same variable content with different names. When you change the value of one of the variables, the other also changes. "&" Is used for reference assignment.

 "; Echo $ I; // output variable $ I;?>

Code running result:

The difference between referencing and assigning values is that assigning values copies the original variable content and saves it with a new memory space, while referencing gives the variable content a name. It is equivalent to an alias. It is as if some literary enthusiasts often contribute articles to newspapers and magazines, but generally they do not have real names, but use pen names. this pen name can be considered as a reference.

After learning about variables, let's take a look at the scope of variables in the next section ".

Related video tutorial recommendations: php1.cn solitary nine cheap (4)-php video tutorial PHP variable creation and naming rules

The above is a detailed description of the definition and use of PHP variables. For more information, see other related articles in the first PHP community!

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.