FAQ: PHP Strings and Variables - Assign by Reference


This community-built FAQ covers the “Assign by Reference” exercise from the lesson “PHP Strings and Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Assign by Reference

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I thought I understood the concept of By Reference, but the following code example confuses me.

/* Imagine a lot of code here */  
  $very_bad_unclear_name = "15 chicken wings";

// Write your code here:
	$order =& $very_bad_unclear_name;
	$order .= ", 20 drumsticks";
// Don't change the line below
  echo "\nYour order is: $very_bad_unclear_name.";

The result of the echo statement is: “Your order is: 15 chicken wings, 20 drumsticks”; but I would expect the result to be “Your order is: 15 chicken wings”.

As described in the lesson:

When we assign by reference we’re saying that the variable on the left of the operator should point, or refer , to the exact same data as the variable on the right.

After all, we are assigning
$order =& $very_bad_unclear_name,
and not
$very_bad_unclear_name =& $order.

So how does assigning or reassigning anything by reference to the $order variable affect the contents of the $very_bad_unclear_name variable?

1 Like

Here is the whole paragraph you qouted:

When we assign by reference we’re saying that the variable on the left of the operator should point, or refer , to the exact same data as the variable on the right. With assignment by reference, changes made to one variable will affect the other

I added the boldface for emphasis. Essentially you are giving the variable in question an alias when you assign another variable by reference. Using either name in any operation affects both variables. The variables are like labels for the actual data. If the data were in a drawer, and then you came along and labelled the drawer drawer_A, and after that I added an additional label to the same drawer calling it drawer_1, either of us could use either label name to refer someone to the same drawer.

1 Like

@midlindner - Thanks, that makes sense.

In other languages, I have always used By Reference in a way that mirrors the example given in the lecture code, i.e. you want the reassignment reflected further down in the code, not the original assignment.

$first_player_rank = "Beginner";
$second_player_rank =& $first_player_rank; 
echo $second_player_rank; // Prints: Beginner

$first_player_rank = "Intermediate"; // Reassign the value of $first_player_rank
echo $second_player_rank; // Prints: Intermediate

I never realized that By Reference changed both variables until I came across the exercise code I posted above. I am glad I decided to go through this course as a refresher to PHP, as I learned something new!

1 Like

without this command =& does the .= command have any value?

1 Like

= is the assignment operator, so the variable on the left side would be assigned the value on the right side. Consider:

$a = 1;
$b = $a;
$a = 2;
echo $b; //prints 1

$b =& $a;
$a = 5;
echo $b; //prints 5

Hope this helps.