On the value types and reference types of PHP variables

Source: Internet
Author: User

See the user in the discussion of PHP & symbols, to thoroughly understand its usage, it is necessary to discuss the two forms of variables.

PHP variables are stored in memory, and the variables are not directly the contents of the value, but the address. For example:

$a = 1;

We look as if the variable $ A directly stores the value of 1. In fact, the PHP interpreter creates a variable $ A, places the value: 1 somewhere in memory, and then stores the address of the value in the variable $ A.

When you need to take a value, find the address in the variable $ A, and then find the value of the variable based on the address.

Look down:

echo $a;

The output 1,php interpreter will do this like this: find the address stored in $ A, and, depending on the address, find the value that exists somewhere in the memory and output it to the screen.

It looks like a simple line of code, but that's the process.

Then look down:

$a = 1; $b = & $a;

Here's the variable $b does an interesting operation,& the symbol takes out the address stored in the $ A variable and stores it in the $b variable.

So, if you use the following code:

Echo $b;

The result will be that the output 1,php interpreter first takes out the address stored in the $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 change. In turn, changing the value of the $b will change the value of the $a.

In this step, we can be sure that the variable $ A and $b store the same address and point to the same value.

So, we can conclude that they represent the same variable.

Further summing up, it can be concluded that if two variables store the same address, they are the same variable.

Having learned something, we begin by describing value types and reference types.

Let's start by looking at the following code:

$a = 1; $b = $a;
$a = 2;echo $b; Output 1

Assigning a value of $ A to $b, after changing the value of $ A, the value of $b remains the same, meaning $ A and $b are two different variables, pointing to different addresses. This assignment creates different variables in the form we call value types.

Look again:

Class User{public $name = ' Tome ';} $a = new User; $b = $a; $a->name = ' Jim '; Echo $b->name; Output Jim

The value of $ A is also assigned to $b, and after the change of $ A, the $b changes, which means that $ A and $b are the same variable, pointing to the same address. This assignment does not create a new the form of the variable we call the reference type.

In PHP, most variable types, such as strings, integers, floats, arrays, and so on, are value types, and classes and objects are reference types, which you need to be aware of when you use them.

On the value types and reference types of 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.