pass by reference
View Single Post
05-06-2009, 11:31 PM
Join Date: Sep 2007
Nothing besides the variable we are pointing to has changed, your "assigned by reference" variable is doing its job and referring you to your other variable, no matter what the content may be.
Really long answer:
I have been itching to write something on this for ages now, so here it goes:
Passing by reference is using what is called “Pointers” in more advanced languages. I am not familiar with what PHP lets you do with pointers, so I won’t go too far in depth with anything. To understand the concept of pointers, you have to be familiar with how variables and their values work behind the scene.
How Variables Work
To start, I have to make sure we are all past a common misconception. Most people think that variables
values; they are not. All values are kept in the computers RAM where the variables point to. These attributes are normally hidden to the user, so when you assign a variable you actually modify a value in the ram.
How RAM Works
This is where the computer stores temporary data for fast access and modification. You can look at ram as a grid, every byte has a given spot and each spot has a unique identifier that refers to it. Instead of counting rows and columns on a base ten system (1,2,3,4,5,6,7,8,9,10,11…..), computers count ram spots using a base-16 number system*, so the combination of letters and numbers you may see are actually numbers on a different counting system.
Here is what a section of ram could look like:
As you can see, each address holds a single byte, that byte translates to data that we use.
How this ties together:
Suppose you have the following section of code:
The first thing the processor does is assign a portion in memory to the given value. So a particular section of your ram could look like
The computer then assigns the address that $var is pointing at to “a020c290”. This is
transparent to you, every time you use $var you access that address in memory and output 10, the computer takes care of this for you.
Since these variables that you are using merely point to memory, why can’t you have a variable whose value (in memory) is another memory address? The answer is that you can, they are called pointers. Instead of being meaningful data, these variables contain data that redirects (or points) the computer to another address in memory.
So suppose you have the following code:
The computer sets the following in memory (
represents a memory address value, the system uses a complicated system to discern between an address and value)
Notice that the value of memory address 2 is the
of memory slot 1. This points whatever is going on to memory address 1. Therefore if you do the following
The memory will be changed to:
This changes the value of memory address 1, but leaves memory address 2 pointing to memory address 1.
This means that next time you call memory address 2 (the pointer), you will be redirected to address 1, making it appear that the value of the pointer has changed
*The first 30 number of a hexadecimal (base 16) number system (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,10,11,12,13,14,15 ,16,17,18,19,1a,1b,1c,1d)
View Public Profile
Send a private message to Village Idiot
Find More Posts by Village Idiot