I'm just doing some reading of the PHP manual, trying to fully grasp
the method of writing functions. I think I'm missing the point of a
very important..erm...point. =)
What is the difference between passing an argument by value and
passing an argument by reference? I'm having trouble wrapping my head
around this.
Consider the following function:
function increase($numberToIncrease) {
$numberToIncrease++;
}
My function increase() accepts a number as a parameter, increments it
and then does nothing. It doesn't return the value. It just stops
running. Now, with that in mind, consider this code:
$num = 0;
increase($num);
echo $num;
What is the value of $num? It's 0. The reason for this is because PHP
took the value contained in $num and *copied* it to the function
parameter $numberToIncrease. Now there are two instances of the same
value. When the increase() function increments the $numberToIncrease,
it's value is destroyed upon the function terminating and the whole
time $num is never touched.
This behavior can be modified if you set your function to accept
parameters by reference. This is done simply by placing an ampersand
(&) in front of the parameter you want to *reference*.
function increase(&$numberToIncrease) { // notice the & in front of
the parameter
$numberToIncrease++;
}
Now when we execute the same code as before, we get an entirely
different result:
$num = 0;
increase($num);
echo $num;
Now the value of $num has been increased. This is due to the fact that
we passed the value by *reference* instead of passing it by value. PHP
now looks at the exact memory location that the source variable came
from, modifies it, and ends the function.
You can see this also when passing any value by reference.
Consider this code:
$a = 'foo';
$b = $a;
$b = 'bar';
echo $a;
$a has a value of 'foo' still because when we copied it's value into
$b, it was a literal copy. Now lets add the reference operator in
front of the equal sign to insure that it's passed by reference.
$a = 'foo';
$b =& $a;
$b = 'bar';
echo $a;
I hope this helps you understand the concept of references better.
It's a really great tool to understand as you develop more complex
applications.
Cheers,
Mike G.
When:
$a = 'foo';
$b =& $a;
$b = 'bar';
echo $a;
the original value of $a is lost? Why not just say $a = 'bar' to begin
with?
As for the original value of a, it was always there. $a and $b are
essentially the same thing when it comes to references. $a has the
value. $b *references* the value of $b. $b has no value on it's own --
it's more like an alias to $a. Therefore when $b is changed, it really
changes $a.
References become very powerful when using them as arguments to
functions. Also, when it comes to OOP. The way object instances are
passed changes with PHP 4 and PHP 5.
How is that possible when:
> > $a = 'foo';
> > $b =& $a;
> > $b = 'bar';
> > echo $a;
which outputs 'bar'?
Does that help?
THOM
On Jul 27, 6:40 pm, clintonia <clinto...@gmail.com> wrote:
> > > with?- Hide quoted text -
>
> - Show quoted text -
Thanks!
When you pass by value you are saying "Here is the value of my
variable. Do your calculations with it." In the example Mike provided.
$num = 0;
increase($num);
echo $num;
function increase($numberToIncrease) {
$numberToIncrease++;
}
Here we are giving the value 0 to the function increase. It has no
effect on our variable at all. We are giving it the value to work with
and it is creating it's own variable to work with.
Now, if we pass it by reference, we are saying "Here is the memory
location of my variable. Go get it and do your calculations." So now
the function "increase" has my variable and whatever calculations it
does affects that variable.
I hope that makes a little more sense. This is the way it was told to
me and it was much easier to understand afterwards. Hope it helped!
hmmm, variable scope...I've just done a quick lookup of that topic in
online PHP manual. You're assumption that I'm not clear on that is
correct. =)
I'll be re-reading that until I get it, I suppose.
Jason -
That does help a little, and after going back through some notes I
have on references, I think I'm starting to understand better (slowly
but surely).
$a =4;
$b=&$a;
$a=6;
echo $b //Outputs 6
So, $b is going to equal the last value for which $a is set, no matter
where $b=&$a is placed in the code? Is that correct?