473,626 Members | 3,413 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 1535
mo******@ecircl e-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******@ecirc le-ag.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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******@ecircl e-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
10115
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 code... TCHAR myArray; DoStuff(myArray);
6
1303
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 no, then where will the array elements exist? since items inside the array are value type they have to be on the stack of the executing thread, but the array itself will be on the heap since its a reference type.
7
1721
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 != copy.Length; i++)
5
2712
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? Thanks in advance. David
16
16385
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
3365
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 array. When I try it with the following code i get these errors 1. Use of possibly unassigned field 'micData' 2. The out parameter 'rem' must be assigned to before control leaves the current method Could someone point out how to do this and what...
7
3407
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 function that prints the elements of an array. don't use pointer to an array as parameter because pointer will be copied, use reference to the array instead.
4
1693
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 this is pseudocode of what I want to do: <?php $m_fieldItems = createObjects(); $outstr = ''; for($i=0; $i < count($m_fieldItems); $i++)
275
12238
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
8272
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6126
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1516
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.