472,961 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1497
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.