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. 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.
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
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
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.
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.
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.
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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"
|
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...
|
by: Dylan |
last post by:
thanks
|
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...
|
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...
|
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...
|
by: SAI |
last post by:
Both have "TextBox" element and "Runat server". I don't understand the
difference. Please advise. Thanks.
|
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
|
by: iceman19860106 |
last post by:
hello everyone!
what is the difference between NULL and 0 in C language?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |