Internals of PHP references

Hi all,

I’m trying to broaden my PHP knowledge and in doing so I have been looking into the workings of references on variables. I know that values are stored in C zval structure that contains fields for determining if the value is a reference value and how many references is has got. I also know that the actual value is stored in a C union.

What I’m not quite sure about however is how a PHP variable gets or sets a value through the zval structure. Here is how I understand what happens when a variable is initialized. PHP allocated space in memory, creates a zval structure and stores information about the newly assigned value into a union that is than pointed to from the zval struct. But how does PHP actually go about retrieving the value from memory? How does PHP exactly know that a certain zval structure is tied to a certain variable? Does the variable hold a pointer value that points to the struct? And if so, how are those pointer values stored in memory?

Anybody who can shine a light on this? Thanks in advance!