473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Any difference between these two?

Is there anything funcationally difference between the following
snippets:

// first
$lines[] = array();
$line =& $lines[count($lines) - 1];

// second
$line = array();
$lines[] =& $line;

They should do the thing. A voice in my head though is saying there's a
subtle difference.

Dec 7 '05 #1
10 1354

Chung Leong wrote:
Is there anything funcationally difference between the following
snippets:

// first
$lines[] = array();
$line =& $lines[count($lines) - 1];

// second
$line = array();
$lines[] =& $line;

They should do the thing. A voice in my head though is saying there's a
subtle difference.


They look the same to me -- at the end of the day both $line and
$lines[0] point to the same, empty array. Of course #2 is more
readable and straightforward.

Dec 7 '05 #2

Chung Leong wrote:
// second
$line = array();
$lines[] =& $line;


Can anyone tell me what is the 2nd line? I mean, the first one declares
an array named "line", but the 2nd one?
I'm comfused by & and [] there, and as you can imagine, i'm a kind of
beginner right now...

regards - julian

Dec 7 '05 #3
NC
Chung Leong wrote:
Is there anything funcationally difference between the following
snippets:

// first
$lines[] = array();
$line =& $lines[count($lines) - 1];

// second
$line = array();
$lines[] =& $line;


Yes. The first example assumes that the array is numeric and there are
no gaps in numbering. If there are gaps, $lines[count($lines) - 1]
will not return the last element in the array. Consider this snippet:

$lines = array('Zero', 'One', 'Two', 'Three', 'Four');
unset($lines[1]);
print_r($lines); /* Outputs:
Array
(
[0] => Zero
[2] => Two
[3] => Three
[4] => Four
) */
echo $lines[count($lines) - 1], "\r\n"; // outputs "Three"...

The second example makes no such assumption and simply adds another
element to $lines...

Cheers,
NC

Dec 7 '05 #4
JDS
On Tue, 06 Dec 2005 18:48:03 -0800, Chung Leong wrote:
funcationally


Is that like, as pertaining to a really fun vacation?

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Dec 7 '05 #5
ZeldorBlat wrote:
Chung Leong wrote:
Is there anything funcationally difference between the following
snippets:

// first
$lines[] = array();
$line =& $lines[count($lines) - 1];

// second
$line = array();
$lines[] =& $line;

They should do the thing. A voice in my head though is saying there's a
subtle difference.


They look the same to me -- at the end of the day both $line and
$lines[0] point to the same, empty array. Of course #2 is more
readable and straightforward.


I think you're right. My concern was that in the first block, an array
was added to $lines, while in the second, a reference was added.
Momentarily I forgot that references don't exist as discrete objects.

Thanks.

Dec 7 '05 #6

Chung Leong wrote:
ZeldorBlat wrote:
Chung Leong wrote:
Is there anything funcationally difference between the following
snippets:

// first
$lines[] = array();
$line =& $lines[count($lines) - 1];

// second
$line = array();
$lines[] =& $line;

They should do the thing. A voice in my head though is saying there's a
subtle difference.


They look the same to me -- at the end of the day both $line and
$lines[0] point to the same, empty array. Of course #2 is more
readable and straightforward.


I think you're right. My concern was that in the first block, an array
was added to $lines, while in the second, a reference was added.
Momentarily I forgot that references don't exist as discrete objects.

Thanks.


Yeah -- I like PHP's reference model a lot, mainly for it's simplicity:
every single "variable" is just a "label" for some value in memory.
Creating a "reference" is really just a way of creating another label
for that same value in that same spot in memory. Of course there are
some rules about what is always assigned by reference and what is
always copied.

Dec 7 '05 #7
ZeldorBlat wrote:
Yeah -- I like PHP's reference model a lot, mainly for it's simplicity:
every single "variable" is just a "label" for some value in memory.
Creating a "reference" is really just a way of creating another label
for that same value in that same spot in memory. Of course there are
some rules about what is always assigned by reference and what is
always copied.


Well, simple logic could leads to scenarios that aren't that easily
understandable. My original question arose out of this function I'm
writing, which returns a large array of arrays. Knowing that PHP's
copy-on-write mechanism creates copies when "references" are returned,
I didn't want to return a whole array-ful of them. The line $lines[] =&
$line did look like that's what being created.

Dec 8 '05 #8
ZeldorBlat wrote:
Yeah -- I like PHP's reference model a lot, mainly for it's simplicity:
every single "variable" is just a "label" for some value in memory.
Creating a "reference" is really just a way of creating another label
for that same value in that same spot in memory. Of course there are
some rules about what is always assigned by reference and what is
always copied.


Well, simple logic could leads to scenarios that aren't that easily
understandable. My original question arose out of this function I'm
writing, which returns a large array of arrays. Knowing that PHP's
copy-on-write mechanism creates copies when "references" are returned,
I didn't want to return a whole array-ful of them. The line $lines[] =&
$line does give the impression that that's what being created.

Dec 8 '05 #9
ZeldorBlat wrote:

Yeah -- I like PHP's reference model a lot, mainly for it's simplicity:
every single "variable" is just a "label" for some value in memory.
Creating a "reference" is really just a way of creating another label
for that same value in that same spot in memory. Of course there are
some rules about what is always assigned by reference and what is
always copied.

And they're ENORMOUSLY different between PHP 4 and PHP 5.

Colin
Dec 8 '05 #10
julian_m wrote:
Chung Leong wrote:
// second
$line = array();
$lines[] =& $line;

Can anyone tell me what is the 2nd line? I mean, the first one declares
an array named "line", but the 2nd one?
I'm comfused by & and [] there, and as you can imagine, i'm a kind of
beginner right now...

regards - julian


The second line adds a *reference* to the variable $line to the end of
array $lines; or to put it another way, it makes $lines[0] another name
for the same thing as $line. This means that even if the code later said

$line = 22;

(perfectly valid, though odd - it simply forgets about the array)
$lines[0] will have value 22.

If the '&' weren't there, then after
$line = 22,

$lines[0] will still be an array.

Colin
Dec 8 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
11
by: ATSkyWalker | last post by:
What's the difference between these 2 statements? If you have a String s="12345" s = "54321" But s = "5432"
19
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
4
by: SAI | last post by:
Both have "TextBox" element and "Runat server". I don't understand the difference. Please advise. Thanks.
7
by: Bart_D | last post by:
Hi, Can anybody explain me what's the difference between for example: imports system.data implements ICallbackEventHandler inherits System.Web.UI.Page Thanks Bart
5
by: iceman19860106 | last post by:
hello everyone! what is the difference between NULL and 0 in C language?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
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
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...

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.