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
9 2140
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
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.
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]
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
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.
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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 :...
|
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...
|
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...
|
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://" +...
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |