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

PHP

Using & 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)); }

?>

Sorry, it didn't keep my formatting when I posted.

2 Answers

Hi 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.

Hmm. 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.

Tell 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.

The & 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.

Hi 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;

?>

The 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.