I read a lot of articles about how to construct the variables inside the machine Zend and found one interesting thing that I can not explain:
$int = 100;
xdebug_debug_zval('int'); /// int:(refcount=1,is_ref=0),int 100
$int = &$int;
xdebug_debug_zval('int'); /// int:(refcount=1,is_ref=1),int 100
As it turns out that we are creating the link itself to itself? How is it possible?
Clear information from what I know:
As a rule is_ref = 1
only when the container refers zval two or more variables of hard link.
refcount
- is the number of variables refer to the same zval container, but the difference is that the refcount for different works with is_ref = 0
and is_ref = 1
.
If is_ref = 0
, and refcount > 1
when creating hard links, we get a new zval container, if we do the assignment by a value - new zval container will not be created.
If is_ref = 1
, and refcount > 1
when creating hard links new zval is not created, used old. if we do not create a hard link, but do assigning by a value - it means what we created new zval container.
P.S I wrote this in order to show that understand that I ask and show why I do not understand the behavior of the code that I have written above
A variable cannot reference itself in the expression.” This error is due to a restriction in the Set Variable Activity to self-reference a variable. As a workaround, you can create another variable that you will do your activity and then assign the result to your initial variable.
The most simple form of storage is called a variable. It's an area of memory that stores one item of data, such as a number or a character.
Variables allow us to store, change and access this information as the program runs. Variables can store all sorts of information.
r-value: It refers to the data value that is stored at some address in memory.
The answer is pretty simple, as explained in the comments to your question. Though, I think I understand where your confusion comes from, so let's break this down. :D
First you assign a value to a variable, internally PHP stores this in a memory segment and increases the counter of variables which refers to this address. (Ref count = 1). All straight forward up until this point.
Then, you re-use the variable to store a reference (pointer in C-terms) to this memory address. The PHP manual explains this as storing a reference to a variable, to make things easier for non-C programmers, which is where (I think) your confusion comes from. There is no such thing as a reference to a variable in the internals, just the data the variable is linked to. Since you re-used the variable to store this reference, the reference count doesn't increase: There is still just one variable pointing to this memory segment. However, it is no longer a normal PHP variable, but a reference (pointer) to the data.
Edit, added:
Another way to accomplish the same result, is by using two variables and then unset
the first one. Code example:
$a = 100; // refcount += 1
xdebug_debug_zval ('a'); // refcount=1,is_ref=0 -> zval {value=100,type=int (addr=0x78765asd)}
$b =& $a; // refcount += 1
xdebug_debug_zval ('a') // refcount=2,is_ref=0 -> zval {value=100,type=int (addr=0x78765asd)}
xdebug_debug_zval ('b') // refcount=2,is_ref=1 -> zval {value=100,type=int (addr=0x78765asd)}
unset ($a); // refcount -= 1
xdebug_debug_zval ('b') // refcount=1,is_ref=1 -> zval {value=100,type=int (addr=0x78765asd)}
Using only one variable merges the two operations into one, without destroying the data. Thus: 1 variable (refcount=1), which is a reference (is_ref=1) to the data itself.
As we've tried to explain to you the confusion stems from the fact that the premise behind the original question is flawed: You're not referencing a variable in these examples, you're referencing an area of memory which contains the data originally associated with said variable. So you're overwriting one (the original) "hardlink", with another. Only difference is that the latter is marked as such, for internal PHP reasons. (References does not get copied on write, in the case of refcounts > 1.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With