What is the difference between a mutable variable and a reference assignment in PHP?

Source: Internet
Author: User
From the perspective of the PHP kernel to explain it to you, you can understand in depth, it is fully understood;
PHP variables in the kernel are stored by the C language structure Zval (you have not learned C, you can be understood as an object, the member variables are the properties of the class, for the time being understood first), the ZVAL structure is as follows:

struct _zval_struct {zvalue_value value;//storage variable zend_uint REFCOUNTGC;//= reference count defaults to: 1zend_uchar type;//variable-specific type Zend_uch AR IS_REFGC; Indicates whether it is a reference};

As an example, the PHP code is as follows:

$a = ten; $b = $a;

At this time $b no reference, just give a value of $ A $b, then zval the structure of the REFCOUNTGC becomes 2, and IS_REFGC or false, indicating unreferenced, because the kernel $ A has opened up a memory space, when $ A is assigned to $b, the value of $b only points to the $ A, this eliminates the re-opening of a piece of memory, but when the $b value is changed ($a value will not change), $b will open up a new memory space, this is how to copy, PHP variables in the kernel How to store, the following reference:

$a = ten; $b = & $a;

At this time, the kernel stores a $ A structure IS_REFGC labeled true, the Representative is a reference, then $ A and $b all point to the same memory address, when $b=20, the $a will become 20

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.