#26396 [WFx]: foreach scope modality

From: Date: Wed, 26 Nov 2003 22:12:54 +0000
Subject: #26396 [WFx]: foreach scope modality
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-51123@lists.php.net to get a copy of this message
 ID:               26396
 Updated by:       helly@php.net
 Reported By:      php dot net dot 1 at odi dot ch
 Status:           Wont fix
 Bug Type:         Arrays related
 Operating System: *
 PHP Version:      4.3.3
 New Comment:

PHP variables are implemented as refcounted unions in c. A reference in
PHP means a flag in php which is the difference of making copies or
working on the original memory. PHP Objects are handles in PHP 5 so
copying it doesn't make a difference - only the handle is copied.


Previous Comments:
------------------------------------------------------------------------

[2003-11-25 18:48:12] php dot net dot 1 at odi dot ch

Sad to see that PHP's language constructs are so fundamentally flawed.

------------------------------------------------------------------------

[2003-11-25 18:11:31] jpatrin at pnicorp dot com

Ok, I'll accept that response, but why does foreach not make a copy of
the referenced array? I see no place in the foreach docs that say that
it doesn't make a copy when the variable is a reference.

Sidenote: I thought that all PHP vars were refernces and that usinf =&
made it a refernce to the same object instead of a refernce to a copy
of the object. If this is true, the copy should still be made just
fine. foreach is ALWAYS supposed to make a copy of the array and
foreach over that.

------------------------------------------------------------------------

[2003-11-25 17:21:46] helly@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

I was right then. Global creates a reference and referenced arrays
cannot be nested. When an array is passed to foreach and it is not a
reference then a copy of the array is created. That's where the
difference comes from.

------------------------------------------------------------------------

[2003-11-25 16:27:47] jpatrin at pnicorp dot com

Here's a bit more. If you use 

$usr_langs =& $GLOBALS['usr_langs'];

instead of

global $usr_langs;

the same bug presents it self.

Also, if you put

global $usr_langs;

above the echo "Test2..." You get only "de" in the output. It seems
like global is munging the scope of foreach copies.

------------------------------------------------------------------------

[2003-11-25 16:08:19] helly@php.net

Interesting, anybody?

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/26396

-- 
Edit this bug report at http://bugs.php.net/?id=26396&edit=1


Thread (10 messages)

« previous php.bugs (#51123) next »