PHP variable quoted by doubt

Source: Internet
Author: User
Doubts about PHP variable references
I thought I had a very clear reference and assignment to the variable, and the result met the following question, completely subverting my understanding.


$str = ' Wangchuanbo ';
$s = & $str;
Unset ($STR);
echo $s, ' Hello world ';
?>

Results $s output or wangchuanbo, not the same block memory address, the original variables are unloaded, reference variables have value??
PHP Variable Reference


------Solution--------------------
Zhang Fei's surname is a flying word wing de
That is, Zhang Fei and Zhang Yide are the same person
You killed Zhang Fei, just put the name of Zhang Fei to erase the world, and that person still in Zhang Yide this title everywhere wandering
------Solution--------------------
Unset a reference, just breaks the binding between the variable name and the variable content
$s = & $str; in PHP, it means
$s and $str all point to the same place, C.
After unset, $str can't find C.
I'll give you another example.
Xiao Zhang listens to Xiao Li and says that the treasure is on the sea
Xiao Li lost his memory, but Xiao Zhang still knows that the treasure is at sea

In C + +, references mean that even memory addresses are the same
After all, it's a constant pointer.
------Solution--------------------
To put it simply
The pointer to C is pointing to a specific address in memory.
The pointer to C has the concept of a data type, so pointers can be involved in the operation
The PHP code is not compiled into machine code, so there is no access to the memory address.
It is true that the reference mechanism of PHP is very similar to the pointer C, if not counting the pointer, then at least in the performance of the pointer is the same

------Solution--------------------
First you have to understand the PHP garbage collection mechanism, you unset out a variable is not erased from memory! The garbage collection of PHP is done by GC!
------Solution--------------------
Top Sticker Collection ~ ~


------Solution--------------------
References:
Reference: unset a reference, just breaking the binding between the variable name and the variable content
$s = & $str; in PHP, it means
In C + +, references mean that even memory addresses are the same
...

Of course, if you have to understand that, hehe.
You have a folder on the F disk FFF, right-hand "send to =〉 desktop shortcuts"
Then the name is AAA, and then it's called BBB.
Open AAA build a txt inside, and then open the BBB inside also have txt
No, that's what happened.


------Solution--------------------
6 floor. To understand this, you need to understand PHP's garbage collection mechanism

PHP variables are not stored as PHP variables in the Zval organization as defined in the structure
typedef struct _ZVAL_STRUCT Zval;
...
struct _zval_struct {
/* Variable Information */
Zvalue_value value; /* Value */
Zend_uint refcount__gc;
Zend_uchar type; /* Active type */
Zend_uchar is_ref__gc;
};
There are four fields in the Zval structure, which have the following meanings:

Property name means default value
REFCOUNT__GC = reference count 1
IS_REF__GC indicates whether to refer to 0
Value of the stored variable
Type variable-specific types

Unset () does not directly destroy variables, only when refcount=0 is recycled by PHP's garbage collection mechanism

$a = 10;
Xdebug_debug_zval (' a ');
Output:a: (Refcount=1, is_ref=0) =10
$b = & $a;
Xdebug_debug_zval (' a ');
Output:a: (refcount=2, Is_ref=1) =10
$a = 20;
Xdebug_debug_zval (' a ');
Output:a: (refcount=2, Is_ref=1) =20
Unset ($b);
Xdebug_debug_zval (' a ');
Output:a: (Refcount=1, is_ref=0) =20



------Solution--------------------
In fact, a lot of people in the interpretation of the reference like that PHP's underlying implementation of the matter, in fact, this is not right
Explain it with plain truth, not deep-seated principles. Otherwise, the more you talk, the more messy
Just like any C language book does not use assembly language to explain the pointer, because the upper levels do not understand how the bottom can understand

The PHP reference behaves as if it were a pointer. So it's no big deal to think of it as a "pointer."
It's just that you don't use references as "pointers" to arithmetic.

A different way to understand it is
The pointer is a house number for every store in the mall, so he can clearly know where the next one is.
The quote is in and out of the shop's signboard, so he doesn't know who's next.
In addition, the performance of the two is no different
------Solution--------------------
  • 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.