Definition of PHP variables and usage examples

Source: Internet
Author: User
What is a variable?

A variable refers to the amount of value that can change during the execution of a program, as opposed to the constant we are talking about, and the constant is defined so that it cannot be changed in the program. The variables can be changed.

What do variables use to do?

Variables are used to hold our values, equivalent to a container for storing data, such as

x=5;

y=6;

Z=x+y,

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

In PHP, these three letters are called variables.

Assigning values to variables

Variable assignment, refers to a variable specific data values, for strings and number types of variables, you can use "=" to achieve the assignment, like the example above, X, Y is a variable, then 5 and 6 is the value of the variable.

Understand the role and assignment of variables, and then look at how variables are defined

Definition of a variable

All variables in PHP start with "$"

Like this.

$a =5; $b =6

The above example completes our definition of the variable and assigns the value.

Variable names are case-sensitive, and variable names can be letters or underscores, followed by any letters, numbers, or underscores, and the names of variables such as the following are correct:

<?php$thiscup= "Onik"; $_class= ' roof ';? >

Look at the following incorrect variable name:

<?php$1111_car=11112;$ @spcn = ' span ';? >

Variable names cannot start with a number and then there is a variable name that cannot begin with a character other than a letter and an underscore, so the definition of the variable above is wrong.

Although PHP variable names can begin with an underscore, we generally do not recommend this, because the variables that begin with the underscore are usually the variables that are in the PHP system itself.

In addition to the above variables directly assigned, there are two ways to declare variables and assignments, one is the assignment between variables , what does it mean? Look at the following example

Example

<?php$str1= ' SPCN '; $str 2= $str 1; $str 1= ' php '; echo $str 2;? >

Code Run Result:

Instead of assigning a value directly to the variable $STR2, we assign the variable $STR1 to $STR2. This is the assignment between our variables.

The other is a reference assignment. starting with PHP4, PHP introduces the concept of "reference assignment", which is to access the same variable content in different names. When you change the value of one of the variables, the other changes as well. Reference assignments Use "&" to represent references.

<?php$i= "SPCN";                       declaring variable $i$j=& $i;                          Using a reference assignment, $J has been assigned a value of spcn$i= "Hello, $i";                  Re-assigns the Echo $j to the $j;                         Output variable $j;echo "<br/>"; echo $i;                         Output variable $i;? >

Code Run Result:

The difference between a reference and an assignment is that the assignment is to copy the contents of the original variable and then save it with a new memory space, whereas the reference is the name of the variable's content. Equivalent to an alias. It is as if some literary lovers often send manuscripts to newspapers and magazines, but generally they do not have real names, but instead use a pseudonym, which can be regarded as a reference.

Knowing the variables, the next section, let's look at the scope of variables.

Related video tutorial recommended: "Php.cn lonely Nine Cheap (4)-php video tutorial" PHP variable creation and naming rules

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.