473,499 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1523
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10042
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
1293
by: Abhishek Srivastava | last post by:
Hello All, In .Net Array is a reference type and an int is a value type. If I create an array of int, then will the items inside the array get boxed? If yes, it will be a terrible overhead. If...
7
1711
by: Richard Forester | last post by:
Hello. I need some help understanding what goes on when an array is copied. I create 2 arrays and copy one to the other: int pins = {9, 3, 7, 2}; int copy = new int; for (int i = 0; i !=...
5
2701
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied?...
16
16358
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
11
3349
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
7
3391
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
4
1687
by: Ronald Raygun | last post by:
I want to store objects in ana array and then iterate through the array, retrieving a (reference) to each object in the array, and calling a method on the array. I am not sure how to do it, but...
275
12026
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7007
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
7220
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...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.