|
Posted by "Richard Lynch" on 07/22/05 08:54
On Wed, July 20, 2005 3:39 pm, Surendra Singhi said:
> Hello,
>
> (1)
> When I try this code:
> <?php
>
> $var_global =" stuff";
> function f1() {
> global $var_global;
> $var_local = array(1,2);
> $var_global =& $var_local;
> }
> f1();
> print_r($var_global);
> ?>
> I get the output:
>
> stuff
>
> where as I was expecting junk value or null.
>
>
> My question is that, because the array is created locally and we
> pass it by reference (without copying) so on the exit of function, its
> value
> is lost, but how does the global variable gets back its old value?
>
> Is it something which is not defined by the PHP language reference and is
> implementation specific? And in this case instead of giving the global
> variable some junk value or null value, the implementation decides to
> store
> the old values?
PHP is not C.
The & operator is not, as far as I know, defined for an array assignment
operation.
True, you can use & in the parameter list in some versions to keep PHP
from copying the whole array. But that does not legitimatize what you are
doing, I don't think.
I could be 100% wrong. I've never even *TRIED* to use a reference to an
array because I simply don't want to write code that confusing in the
first place.
That's why I use PHP to build web pages instead of C.
YMMV
--
Like Music?
http://l-i-e.com/artists.htm
Navigation:
[Reply to this message]
|