Passing Arguments

2 views
Skip to first unread message

clintonia

unread,
Jul 26, 2007, 11:23:47 AM7/26/07
to Central Florida PHP
Hi,

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.

Mike G.

unread,
Jul 26, 2007, 11:51:26 AM7/26/07
to Central Florida PHP
When you pass a variable to a function as an argument, PHP *copies*
the passed value into the function argument. When it is assigned by
reference, it will not copy the value but instead will refer to the
original value.

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.

clintonia

unread,
Jul 27, 2007, 11:07:33 AM7/27/07
to Central Florida PHP
Sorry, but the simple examples don't help me understand it any better.

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?

Mike G.

unread,
Jul 27, 2007, 11:49:11 AM7/27/07
to Central Florida PHP
That example simply illustrates that references work everywhere within
PHP. And yes, you're right. For that particular example, it would have
been better off to just use $a.

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.

clintonia

unread,
Jul 27, 2007, 6:40:39 PM7/27/07
to Central Florida PHP
So what if you wanted the original value $a holds, aka 'foo'?

How is that possible when:


> > $a = 'foo';
> > $b =& $a;

> > $b = 'bar';
> > echo $a;

which outputs 'bar'?

Mike G.

unread,
Jul 27, 2007, 8:16:44 PM7/27/07
to Central Florida PHP
In that case, I wouldn't use references.

ParkinT

unread,
Jul 27, 2007, 8:38:52 PM7/27/07
to Central Florida PHP
Although Mike G. did an EXCELLENT job of characterizing the
differences (and advantages), let me paraphrase and provide you the
"Reader's Digest Condensed" version.
Perhaps this will help clear up your understanding:
A parameter passed by value cannot be changed within the function.
All the function has to work on is the same VALUE
A parameter passed by reference can be changed within the function.
The function has been given a REFERENCE (a handle directly to the
variable).

Does that help?

THOM

On Jul 27, 6:40 pm, clintonia <clinto...@gmail.com> wrote:

> > > with?- Hide quoted text -
>
> - Show quoted text -

clintonia

unread,
Jul 28, 2007, 3:31:51 PM7/28/07
to Central Florida PHP
Fair enough. :)

Thanks!

clintonia

unread,
Jul 28, 2007, 3:38:15 PM7/28/07
to Central Florida PHP
Thanks for that and to both of you for your help. A muddy
understanding has now turned into a sort of dirty water one. I'm sure
with some practice, I'll fully understand how it can, or should, be
used.

Jason

unread,
Aug 1, 2007, 12:43:17 PM8/1/07
to Central Florida PHP
Maybe I can help un-muddy the water a little. This is what helped me
to understand this.

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!

Mike G.

unread,
Aug 1, 2007, 1:36:07 PM8/1/07
to Central Florida PHP
One thing that is somewhat related to this thread, and more
specifically related to my increase() example is the topic of variable
scope. Could that be a possible point of confusion for you?

clintonia

unread,
Aug 1, 2007, 6:03:43 PM8/1/07
to Central Florida PHP
Mike G -

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?

Jason

unread,
Aug 2, 2007, 11:28:52 PM8/2/07
to Central Florida PHP
Right, and that is because $b and $a both reference the same memory
location.

Michael Girouard

unread,
Aug 3, 2007, 6:58:17 AM8/3/07
to cf...@googlegroups.com
One key thing to understand while coding PHP is "scope". Scope is essentially an area where named members (like variables) can live.

 

 
For the purposes of this discussion, there is "global scope" and "local scope" (aka: "function scope"). Consider this code:

 
<?php

 
// create a var named $name in the ~global scope~
$name = 'Mike G.';

 
function showName() {
    // render a variable named $name in the scope
    // of this function (doesn't exist)
    echo $name;
}

showName();

That code would do absolutely nothing since we didn't tell showName() what $name was. Functions have their own world that they live in (ie - their own scope), separate from the global scope.

They way you bend that rule is by passing arguments.

// create a var named $name in the ~global scope~
$name = 'Mike G.';

 
function showName($name) {
    // render a variable named $name in the scope
    // of this function (exists by the arg $name)
    echo $name;
}

showName($name);

 
?>

 
This now works wonderfully. The $name of global scope is transferred as an argument into the function and it's value becomes local to that function as well. The problem with this is that there are now two copies of $name.

$name in global scope and $name in local scope.

Since each of these variables are copies of one another (and not references to the same exact piece of data) they operate as independent entities. Thus when you change $name inside the function, $name outside the function is not affected.

If you use a reference as an argument those rules change again. Now they refer to the same piece of memory and are essentially the same thing. 

Hope that helps a bit more. 

Cheers,
Mike G.
--
Mike Geee!
Professional Superhero
mgir...@gmail.com
386-503-3505

Me: www.lovemikeg.com
Us: www.cfphp.org
Reply all
Reply to author
Forward
0 new messages