PHP: foreach($a as $b) vs. foreach($a as &$b)

In PHP, what is the difference between
foreach($a as $b)
and
foreach($a as &$b)
?
LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Whenever you have an & before a $variable, it means that the $variable is being passed by reference.

For example, if you have an array of arrays:
$a = array(
  array("apple","orange"),
  array("hello","world")
);

Open in new window


Then if you were to do this:
foreach($a as $b)
{
  $b[0] = "FOOBAR!";
}
print_r($a);

Open in new window

...then you would see that $a doesn't look changed at all, even though you modified the first element of each of the inner arrays and tried to set it to "FOOBAR!" Your $a array would still look something like:
$a = array(
  array("apple","orange"),
  array("hello","world")
);

Open in new window


This is because without the &, the inner array is being treated as a COPY of the original array. So you're modifying the COPY, and then that copy is thrown away at the end of the loop.

If you were to do this:
foreach($a as &$b)
{
  $b[0] = "FOOBAR!";
}
print_r($a);

Open in new window


...then $a would look like:
$a = array(
  array("FOOBAR!","orange"),
  array("FOOBAR!","world")
);

Open in new window


...because the & means that you're accessing the ORIGINAL array, not a COPY. This is the difference between pass-by-reference (access the ORIGINAL) and pass-by-value (access the COPY).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Radek BaranowskiFull-stack Java DeveloperCommented:
in the first piece of code you take some set of values ($a) and iterate over them picking them one by one as $b

in the second one you are taking references to elements of $a, that means that if you do any manipulation on $v it will be reflected in $a, not only on the single item you "took out" of the set for the current iteration. that includes alteration to $v being caused by the loop instruction

the code showing this behavior:

<?php

$a=['one','two','three'];

foreach ($a as &$b){
	

}

echo $a[0].PHP_EOL;
echo $a[1].PHP_EOL;
echo $a[2].PHP_EOL;


foreach ($a as $b){
	$b = $b+' something else';

}

echo $a[0].PHP_EOL;
echo $a[1].PHP_EOL;
echo $a[2].PHP_EOL;

?>

Open in new window

and resulting output:

one two three one two 0
0
Ray PaseurCommented:
This is a fairly common question for PHP programmers, so we have an article here at E-E to explain it.
http://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.