468,272 Members | 2,194 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

array creation as reference, or always copied?

Throughout the PHP manual, and normal code, one finds this construct:
$var = array( ... );

However, as far as I can tell this is quite inefficient, since it
always means two arrays are being created. The right-side array is
created, and then copied to the left-side variable.

Thus one would expect the normal syntax should be:
$var =& array( ... );

But this is not used in the manual very often.

So, is there some kind of magic on the first array assignment such that
it is done by reference (the first time)? Or are the vast majority of
examples of array creation in the manual (and most PHP code)
inefficient?

Jul 17 '05 #1
3 1334
mo******@ecircle-ag.com wrote:
However, as far as I can tell this is quite inefficient, since it
always means two arrays are being created. The right-side array is
created, and then copied to the left-side variable.

Thus one would expect the normal syntax should be:
$var =& array( ... );

But this is not used in the manual very often.


Using an array by reference would only be useful if you want to re-use it:

// Create new arrays
$array = array(1,2);
$array2 = array(3,4);

// Create a reference
if (somecondition) {
$array_ref =& $array;
} else {
$array_ref =& $array2;
}

// Do something with the array
print $array_ref[0];
JW

Jul 17 '05 #2
<mo******@ecircle-ag.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Throughout the PHP manual, and normal code, one finds this construct:
$var = array( ... );

However, as far as I can tell this is quite inefficient, since it
always means two arrays are being created. The right-side array is
created, and then copied to the left-side variable.

Thus one would expect the normal syntax should be:
$var =& array( ... );

But this is not used in the manual very often.

So, is there some kind of magic on the first array assignment such that
it is done by reference (the first time)? Or are the vast majority of
examples of array creation in the manual (and most PHP code)
inefficient?


array() is a declaration, not a function. That's why you can do things like

function Dingo($baby = array())

and

class Dingo {
var $baby = array();
}
Jul 17 '05 #3
On 15 Feb 2005 23:52:36 -0800, mo******@ecircle-ag.com wrote:
So, is there some kind of magic on the first array assignment such that
it is done by reference (the first time)? Or are the vast majority of
examples of array creation in the manual (and most PHP code)
inefficient?


As other people have pointed out Array() is not a function. But I
thought I should also add that PHP uses copy-on-write for all
operations meaning that there is no performance penalty for passing
arrays (and strings, I believe) around. Only when you modify an
array is a copy actually made. This is all done, of course, behind
the scenes.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Abhishek Srivastava | last post: by
7 posts views Thread by Richard Forester | last post: by
16 posts views Thread by Gerrit | last post: by
7 posts views Thread by arnuld | last post: by
4 posts views Thread by Ronald Raygun | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.