# references in arrays

- Thread: https://externals.io/message/18954
- Original archive: https://news-web.php.net/php.internals/18954
- Started by Ron Korving on 2005-09-15T10:35:48+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #18954 — Ron Korving — 2005-09-15T10:35:48+00:00

URL: https://externals.io/message/18954#18954

Hi,

For performance' sake, I have to know if this is true:

Is it the case that when I do this:

<?php
  $array = array("one" => array(0,1,2), "two" => array(4,5,6));
  $one = $array["one"];
?>

That $one is not a copy, but a reference to $array["one"] and will only
become a copy when I alter the contents of $one? I know this is the case for
regular variables, but does this also go for contents of arrays? If not, I
need to use the ampersand like I used to. But if it really is just a
reference, that would be good to know as I shouldn't be using the ampersand
at all (mistakingly thinking it's a performance advantage when it's actually
not).

Thanks,

Ron

---

## #18956 — Zeev Suraski — 2005-09-15T11:15:02+00:00

URL: https://externals.io/message/18954#18956 · In reply to: #18954

At 13:35 15/09/2005, Ron Korving wrote:
>Hi,
>
>For performance' sake, I have to know if this is true:
>
>Is it the case that when I do this:
>
><?php
>   $array = array("one" => array(0,1,2), "two" => array(4,5,6));
>   $one = $array["one"];
>?>
>
>That $one is not a copy, but a reference to $array["one"] and will only
>become a copy when I alter the contents of $one?

That's correct.

>I know this is the case for
>regular variables, but does this also go for contents of arrays? If not, I
>need to use the ampersand like I used to. But if it really is just a
>reference, that would be good to know as I shouldn't be using the ampersand
>at all (mistakingly thinking it's a performance advantage when it's actually
>not).

Generally, using & where it's not necessary is more often than not a 
performance disadvantage, and not an advantage.  You should never use & for 
better performance, only if you need it for applicative usage.  The engine 
would do its best to do the least copying on its own.

Zeev

---

## #18959 — Ron Korving — 2005-09-15T11:19:00+00:00

URL: https://externals.io/message/18954#18959 · In reply to: #18956

Perfect :)
I've been doing it wrong all the time, thinking I was improving performance.
It's good to know how things really work under the hood.

Thanks,

Ron


"Zeev Suraski" <zeev@zend.com> schreef in bericht
news:5.1.0.14.2.20050915141221.0605b540@localhost...
> At 13:35 15/09/2005, Ron Korving wrote:
> > […]
>
> That's correct.
>
> > […]
I
> > […]
ampersand
> > […]
actually
> > […]
>
> Generally, using & where it's not necessary is more often than not a
> performance disadvantage, and not an advantage.  You should never use &
for
> better performance, only if you need it for applicative usage.  The engine
> would do its best to do the least copying on its own.
>
> Zeev
