473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

$value versus &$value in foreach

What is the difference between these two lines of code. I know in the second I added a & but what does that do exactly?

[PHP]foreach ($arr as $value)[/PHP]

[PHP]foreach ($arr as &$value)[/PHP]

Thanks,
Andrew
Mar 19 '08 #1
9 2140
theS70RM
107 100+
What is the difference between these two lines of code. I know in the second I added a & but what does that do exactly?

[PHP]foreach ($arr as $value)[/PHP]

[PHP]foreach ($arr as &$value)[/PHP]

Thanks,
Andrew

is the difference that one of them creates a parse error? =P

as far as i know (and i am no expert) the & does nothing and is incorrect syntax.


Andy
Mar 19 '08 #2
Markus
6,050 Expert 4TB
is the difference that one of them creates a parse error? =P

as far as i know (and i am no expert) the & does nothing and is incorrect syntax.


Andy
I'm not sure; i've seen it used in other scripts.

Never known what it was for though.

Be useful to get some insight though :)

Regards.
Mar 19 '08 #3
theS70RM
107 100+
check this

http://www.litfuel.net/plush/?postid=8


an addition to php5 it would seem.

pointers, references etc just confuse me, thats why i leave C alone ;)


Andy
Mar 19 '08 #4
I know they are both valid. I did a foreach with the & and it changed the value of my variable but without it it did not!

I have seen the code on www.php.net and actually just found this on http://www.php.net/foreach
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.
So it's a PHP5 thing that passes by reference and can modify your variable.

Uses & so changes $arr
[PHP]
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
[/PHP]
No & so no change to $arr
[PHP]
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
$value = $value * 2;
}
// $arr is still array(1, 2, 3, 4)
[/PHP]
Mar 19 '08 #5
Markus
6,050 Expert 4TB
I know they are both valid. I did a foreach with the & and it changed the value of my variable but without it it did not!

I have seen the code on www.php.net and actually just found this on http://www.php.net/foreach

So it's a PHP5 thing that passes by reference and can modify your variable.

Uses & so changes $arr
[PHP]
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
[/PHP]
No & so no change to $arr
[PHP]
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
$value = $value * 2;
}
// $arr is still array(1, 2, 3, 4)
[/PHP]
Ahhhh, useful!

Regards
Mar 19 '08 #6
TheServant
1,168 Expert 1GB
Good question. I didn't even know the "FOREACH construct used to loop through PHP arrays works on a COPY of the array and not the array itself" so I will definately be using this in the future.
Mar 19 '08 #7
ronverdonk
4,258 Expert 4TB
For your information: there is quite a good explanation about passing values by pointer and by reference in the PHP documentation in chapter References Explained

Ronald
Mar 19 '08 #8
Atli
5,058 Expert 4TB
Good question. I didn't even know the "FOREACH construct used to loop through PHP arrays works on a COPY of the array and not the array itself" so I will definately be using this in the future.
Be careful tho.
As explained in one of the links posted above, even tho the old un-referenced foreach is in fact working on a copy of the array, the internal PHP engine still references the original array until you actually modify the copy.

In the end, referencing the array will trigger internal safe-guards that may cause your code to run slower.

So, only use the referenced version if you plan on actually changing the original data while iterating through it.
Mar 20 '08 #9
TheServant
1,168 Expert 1GB
Be careful tho.
As explained in one of the links posted above, even tho the old un-referenced foreach is in fact working on a copy of the array, the internal PHP engine still references the original array until you actually modify the copy.

In the end, referencing the array will trigger internal safe-guards that may cause your code to run slower.

So, only use the referenced version if you plan on actually changing the original data while iterating through it.
Good point, I will definately do that. Thanks.
Mar 20 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
9
by: Lloyd Dupont | last post by:
Does any of you have the slightest ideas of why you can't modify an array list while in a foreach of its element. I wrote my own collection and I'm trying to add the same behavior but I don't...
1
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls....
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
6
by: Andrew Matthews | last post by:
Hi All, I have the following little class of iterators that allow me to iterate over elements in the file system. I have nested some of them, and then added Func<FileInfo, booldelegates to filter...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
0
by: tpeczek | last post by:
Hi I'm writing windows service which is suppose to acces IIS metabase using DirectoryServices. I'm accesing IIS using the code below: DirectoryEntry iisEntry = new DirectoryEntry(@"IIS://" +...
4
by: raylopez99 | last post by:
I'm reading a book that talks about how some operations, like LINQ queries, are "deferred execution" while other queries are "immediate execution". What is the difference? RL
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.