Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Colton Colcleasure
3,062 PointsUsing & on parameters in functions?
As well as I am using Treehouse, I am also reading a book by Robert Nixon and am currently on the PHP section. In the book it is teaching using the & symbol for parameters in a function.
Here is what he initially says about it:
"In PHP, prefacing a variable with the & symbol tells the parser to pass a reference to the variableās value, not the value itself."
Here is the code. I'm trying to understand what the & is doing.
<?php $a1 = "WILLIAM"; $a2 = "henry"; $a3 = "gatES";
echo $a1 . " " . $a2 . " " . $a3 . "<br>"; fix_names($a1, $a2, $a3); echo $a1 . " " . $a2 . " " . $a3;
function fix_names(&$n1, &$n2, &$n3) { $n1 = ucfirst(strtolower($n1)); $n2 = ucfirst(strtolower($n2)); $n3 = ucfirst(strtolower($n3)); }
?>
2 Answers

Chris Shaw
26,676 PointsHi Colton,
The &
symbol is simply a reference to a variable in memory, whenever you use a reference in PHP you're essentially making a copy of the variable while the value never changes unless you change it it somewhere else, what does that mean?
In your above example you're passing in $a1
, $a2
and $a3
to fix_names()
which has it's parameters bound by references, because they are bound by a reference anytime you change the value it's automatically assigned back to that variables memory pointer and updates without the need to use an return
statement in your function.
You can read more about pass by reference here.

Colton Colcleasure
3,062 PointsHmm. Still not getting it.
What I'm kind of thinking is that if you want to pass a pre-defined variable to a function, you must add the & before the placeholder name in the function. Because if you're not using pre-defined variables in the functions and just passing values, you don't have to use the &.
--Using the & allows you to use variables as parameters in your function. Whereas without &, you can not. Is this correct? I've played with the code quite a bit, trying to many different varieties, but I can't seem to really have an "aha!" moment.

Colton Colcleasure
3,062 PointsTell me if this is correct or not, please.
Simplified, functions perform actions on values passed through them. By using the &, it allows for a variable to be passed through and operations to be conducted on a variable.

Chris Shaw
26,676 PointsThe &
is a special symbol that only relates to pass by reference, for general function use you don't need it and can assign parameters normally, the biggest difference between the two is pass by reference allows you to update the value without returning it while normal parameters clone the value of the variable but don't reassign it to the original parameter.
Without pass by reference
<?php
function a($b) {
// Nothing happens as it's an normal function call
$b = 'World';
}
$a = 'Hello';
a($a);
echo $a;
With pass by reference
<?php
function a(&$b) {
$b = 'World';
}
$a = 'Hello';
a($a);
// `$a` now equals `World`
echo $a;
I think it's very important not to get yourself too caught up in this technique until you have a moderate understanding of PHP as features like pass by reference can be quite confusing.

Jeff Busch
19,287 PointsHi Colton,
Maybe this is over-simplified, but run it and look at the output.
<?php
$a = & $b;
$b = 3;
echo "<br>A: " . $a;
echo "<br>B: " . $b;
$a = 5;
echo "<br>A: " . $a;
echo "<br>B: " . $b;
?>

Colton Colcleasure
3,062 PointsThe results are 3, 3, 5, and 5. Thus, using the & operator really does nothing here except make the two equal to each other constantly. I see how this is different than $a = $b, as that would alter $a, and then later altering $a wouldn't change $b. But it doesn't clarify for me what is happening.
Nonetheless; I appreciate your approach.
Colton Colcleasure
3,062 PointsColton Colcleasure
3,062 PointsSorry, it didn't keep my formatting when I posted.