Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10330 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38386 invoked by uid 1010); 8 Jun 2004 18:52:17 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 38361 invoked by uid 1007); 8 Jun 2004 18:52:16 -0000 Message-ID: <20040608185216.38360.qmail@pb1.pair.com> To: internals@lists.php.net References: <20040608091715.6553.qmail@pb1.pair.com> Date: Tue, 8 Jun 2004 11:53:50 -0700 Lines: 34 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Posted-By: 64.142.6.231 Subject: Re: Confusing pointers in PHP 4 and 5 From: pollita@php.net ("Sara Golemon") > As you can see the original tree now contains a pointer, how can that > be, I didn't change the original array!? > So what I'd like to know is: Is this intended behavior, and if so: why > does it work like this and would it make some operations impossible? > The truth of it is, this was true in PHP4 as well, we just didn't show it at the time, and yes: It's intended behavior. Every "variable" in PHP is a pair. The $foo label that you use to refer to it in your script, and the actual value in memory (referred to as a zval). When you make a reference (using the & operator) the new label is created to point to the same zval as the original label. The zval itself doesn't know which label was first and which came second, it only knows that two labels refer to it in a reference manner (there are non-reference manners as well but let's not confuse the issue here). So when the value is output (regardless of which label was followed to output it), PHP says "This value is referenced by two or more labels." and throws that '&' indicator onto the output. $foo = 1; /* $foo (label) --------> 1 (value) (is_ref=0, refcount=1) */ $bar = &$foo; /* $foo (label) ----------> 1 (value) */ /* $bar (label) -------/ is_ref=1, refcount=2 */ Hope that helps. -Sara