----- Original Message -----
From: "John" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, March 16, 2001 11:09 PM
Subject: Re: [PHP] Strange Behaviour in non-reference globals?
> Hi Yasuo,
>
> Thanks for taking the trouble to explain everything in such detail.
>
> Actually I understood your first message. What it doesn't explain is why:
>
> $gGlobal == 1 is changed permanently to $gGlobal == 55.
>
> in the following code:
It also expected behavior. Here is why.
>
> <?php
> function ChangeGlobalValue()
> {
> global $gGlobal;
>
> $local = 55;
> $gGlobal = $local;
$local is NOT a reference. $gGlobal is reference.
In this case, you are actually changing. $GLOBAL['gGlobal'] in this case.
I think you figures it out why now.
Regards,
--
Yasuo Ohgaki
> print "Value:Global=$gGlobal<BR>";
> }
>
> $gGlobal = 1;
> ChangeGlobalValue();
> print $gGlobal; <=============== THIS IS 55 NOW
>
> ?>
>
>
> ==========================
> ""Yasuo Ohgaki"" <[EMAIL PROTECTED]> wrote in message
> [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> > ----- Original Message -----
> > From: "John" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, March 16, 2001 9:12 PM
> > Subject: Re: [PHP] Strange PHP Reference behaviour with globals
> >
> >
> > > Thanks Yasuo,
> > >
> > > That might explain why $gGlobal is not permanently set to 99,
> > > but that doesn't explain why $gGlobal is set to 55 in
> ChangeGlobalValue()
> > > permanently. So are you or am I missing something?
> >
> > Reference in PHP is not actually a pointer. It's a common pithole for
> C/C++
> > programmers.
> > In C/C++, you will get 99, instead of 55. But we get 55 in PHP. Here is
> why.
> >
> > Reference is just a place holder for a value.
> >
> > Your code creates referece with "global" statement asI wrote in previous
> mail.
> >
> > global $var;
> >
> > creates reference to $GLOBAL['var'] for local $var IN function scope.
> >
> > $var is actually a reference, NOT a variable itself nor a pointer.
> >
> > > > > function ChangeGlobalRef()
> > > > > {
> > > > > global $gGlobal;
> >
> > Creating reference here, for $GLOBAL['gGlobal'].
> >
> > > > >
> > > > > $local = 99;
> > > > > $gGlobal = &$local;
> >
> > You assign reference of "$local" to "$gGlobal" which is also a reference.
> >
> > $gGlobal = &$local;
> >
> > This replaces $gGlobal(reference) with $local(reference).
> > Your code does not change $GLOBAL['gGlobal'], which is the value you want
> to
> > change.
> >
> > > > > print "Ref:Global=$gGlobal<BR>";
> > > > > }
> >
> > Since your code replace reference with reference inside function scope.
> > You get $gGlobal = 99 inside the function, but not outside the function.
> > (Note: Your $gGlobal is refered to $local that has 99 in the function)
> > Because $gGlobal itself is local to the function.
> >
> > Therefore, you cannot get 99 outside the function, but 55.
> >
> > I hope I explained well, so that all readers understand what I'm talking
> about.
> >
> > Regards,
> > --
> > Yasuo Ohgaki
> >
> > > Bye, John
> > >
> > > ""Yasuo Ohgaki"" <[EMAIL PROTECTED]> wrote in message
> > > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> > > > This is expected behavior of PHP's reference. Reference works like a
> > > pointer,
> > > > but it dose NOT works like a pointer. This is case that reference
> does
> > > not
> > > > works as many programmer expected.
> > > >
> > > > I think this is in manual.
> > > >
> > > > Hint: when programmer use 'global $var'. It is the same as do '$var =
> &
> > > > $GLOBALS['var']'.
> > > >
> > > > Regards,
> > > >
> > > > Yasuo Ohgaki
> > > > =========================
> > > > My favorite links
> > > > [RFC] http://www.faqs.org/rfcs/ [W3C] http://www.w3.org/
> > > > [PHP Manual] http://www.php.net/manual/en/
> > > >
> > > > > Hello,
> > > > >
> > > > > Try the following code:
> > > > >
> > > > > <?php
> > > > >
> > > > > $gGlobal = 1;
> > > > > function ChangeGlobalValue()
> > > > > {
> > > > > global $gGlobal;
> > > > >
> > > > > $local = 55;
> > > > > $gGlobal = $local;
> > > > > print "Value:Global=$gGlobal<BR>";
> > > > > }
> > > > >
> > > > > function ChangeGlobalRef()
> > > > > {
> > > > > global $gGlobal;
> > > > >
> > > > > $local = 99;
> > > > > $gGlobal = &$local;
> > > > > print "Ref:Global=$gGlobal<BR>";
> > > > > }
> > > > >
> > > > > print "Global=$gGlobal<BR>";
> > > > > ChangeGlobalValue();
> > > > > print "Global=$gGlobal<BR>";
> > > > > ChangeGlobalRef();
> > > > > print "Global=$gGlobal<BR>";
> > > > > ?>
> > > > >
> > > > > I get the following results:
> > > > >
> > > > > Global=1
> > > > > Value:Global=55
> > > > > Global=55
> > > > > Ref:Global=99
> > > > > Global=55 <=========== shouldn't this be 99 ???
> > > > >
> > > > > Why does setting a global variable to a reference fail in a
> function?
> > > > >
> > > > > Tested on PHP 4.0.4 on IIS5 (CGI version).
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > PHP General Mailing List (http://www.php.net/)
> > > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > > > To contact the list administrators, e-mail:
> [EMAIL PROTECTED]
> > > > >
> > > > >
> > > >
> > > > --
> > > > PHP General Mailing List (http://www.php.net/)
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > > To contact the list administrators, e-mail:
> [EMAIL PROTECTED]
> > > >
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > To contact the list administrators, e-mail: [EMAIL PROTECTED]
> > >
> > >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]