Deep understanding of the value type and reference type of PHP Variables

Source: Internet
Author: User
In PHP, most variable types, such as string, integer, floating point, and array, are value types, and classes and objects are reference types. Pay attention to this when using them.

In PHP, most variable types, such as string, integer, floating point, and array, are value types, and classes and objects are reference types. Pay attention to this when using them.

In PHP, most variable types, such as string, integer, floating point, and array, are value types, and classes and objects are reference types ,, pay attention to this.

To thoroughly understand the usage of PHP, it is necessary to discuss the two forms of variables.

PHP variables are stored in the memory in this way. The variables do not directly store the value content, but the address. For example:

$ A = 1;

It seems that the variable $ a directly stores the value of 1. The actual situation is that the PHP interpreter creates the variable $ a, stores the value 1 in a certain place in the memory, and then stores the value address in the variable $.

When a value is required, first locate the address in variable $ a and then find the value of the Variable Based on the address.

Look down:

Echo $;

1 is output, and the PHP interpreter completes the code like this: Find the address stored in $ a, find the value somewhere in the memory according to the address, and output it to the screen.

It seems like a simple line of code, but the process is like this.

Let's look at the following:

$ A = 1; $ B = & $;

Here, the variable $ B performs an interesting operation, and the & Symbol retrieves the address stored in the $ a variable and stores it in the $ B variable.

The following code is used:

Echo $ B;

The result will also be output 1. The PHP interpreter first retrieves the address stored in $ B and then finds the value based on the address. If we do the following:

$ A = 2; echo $ B; // output 2

You will find that the value of $ a is changed, and $ B is also changed. In fact, changing the value of $ B also changes the value of $.

In this step, we can determine that the variables $ a and $ B store the same address and point to the same value.

We can conclude that they represent the same variable.

Further, we can conclude that if the two variables store the same address, they are the same variable.

After learning about some content, we will begin to introduce the value type and reference type.

Let's take a look at the following code:

$ A = 1; $ B = $ a; $ a = 2; echo $ B; // output 1

Assign the value of $ a to $ B. After changing the value of $ a, the value of $ B remains unchanged. That is to say, $ a and $ B are two different variables, point to different addresses. In this assignment, different variables are created, which are called value types.

Let's look at it again:

Class User {public $ name = 'tome ';} $ a = new User; $ B = $ a; $ a-> name = 'Jim '; echo $ B-> name; // output Jim

Similarly, the value of $ a is assigned to $ B. After $ a is changed, $ B is changed. That is to say, $ a and $ B are the same variable and point to the same address. This value assignment does not create new variables. We call it a reference type.

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.