Re: Re: References and array indexes
| From: | Curt Zirzow | Date: | Thu, 05 Aug 2004 23:57:47 +0000 |
| Subject: | Re: Re: References and array indexes | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-192946@lists.php.net to get a copy of this message | ||
* Thus wrote Ace:
> Gabriel Birke <birke <at> kontor4.de> writes:
>
> >
> > Hello!
> >
> > The following PHP code:
> > <?
> > $a = array("1"=>"First Element", "2"=>"Second
> > Element");
> > $v = &$a['3'];
> > print_r($a);
> > ?>
> > has this result:
> >
> > Array(
> > 1 => First Element,
> > 2 => Second Element,
> > 3 =>
> > )
> >
> > Can anyone give me an explanation for this? What is happening in the
> > guts of the PHP interpreter when it gets these instructions?
> >
> > With best regards
> >
> > Gabriel Birke
> >
>
> Hi !
>
> As you passed $a in reference while defining $v, the '3' index of $a was
> defined. Furthermore, you didn't at any time affect a value to that '3' index
> of $a, but it exists because he was invocated once...
>
> So the result of your print_r isn't odd ;)
>
> Maybe you know that very well and your question was infinitely more technical,
To expand a little further...
the reason why $a['3'] needs to be created is because a reference
*has* to point to something. So php will automatically make a blank
variable for $a['3'] then assign the reference of $a['3'] to $v.
If it was a normal assignment, PHP simply will throw an E_NOTICE
letting you know that you're accessing an undefined variable. Then
create an blank variable that is assign to $v. The array() will
remain unchanged.
Curt
--
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about. No, sir. Our model is the trapezoid!