#39011 [Opn->Bgs]: foreach($_GET as $key => &$value) causes later bugs passing $_GET as a paramete
| From: | tony2001@php.net | Date: | Mon, 02 Oct 2006 10:32:07 +0000 |
| Subject: | #39011 [Opn->Bgs]: foreach($_GET as $key => &$value) causes later bugs passing $_GET as a paramete | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-102979@lists.php.net to get a copy of this message | ||
ID: 39011
Updated by: tony2001@php.net
Reported By: php_bug dot email at email dot digiways dot com
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Windows XP
PHP Version: 5.1.6
New Comment:
>Are you talking about arrays in general, or about my example?
I'm talking about your example, of course.
>If you are talking about my example, then please explain >why the
element in the array becomes a reference if we add >a reference to it?
Because they both now point to the same value. This is how references
work.
See what the docs say:
http://www.php.net/manual/en/language.references.whatdo.php
Previous Comments:
------------------------------------------------------------------------
[2006-10-02 10:09:14] php_bug dot email at email dot digiways dot com
> And since all the elements in the array are references
Are you talking about arrays in general, or about my example?
If you are talking about arrays in general, this is simply wrong, a
simple example can demostrate that.
If you are talking about my example, then please explain why the
element in the array becomes a reference if we add a reference to it?
I.e.
$a = array('foo' => 'bar');
here $a contains the value 'bar' and not the reference to it.
$tmp = &$a['foo'];
Are you saying that now $a has changed and now it contains a reference
to 'bar' and does not contain it as a value any more? Where is this
documented?
------------------------------------------------------------------------
[2006-10-02 08:46:52] tony2001@php.net
>Documentation describes that parameters are passed to >functions by
value by default.
They are passed by value, including all the references in the array.
And since all the elements in the array are references, you're able to
modify them in the function.
See var_dump($myarray);
------------------------------------------------------------------------
[2006-10-01 22:42:59] php_bug dot email at email dot digiways dot com
Documentation describes that parameters are passed to functions by
value by default. And since the behaviour you are talking about is not
documented anywhere, it is definitely a bug.
So, I suggest you read PHP documentation starting with
http://www.php.net/manual/en/functions.arguments.php
which clearly states:
===
By default, function arguments are passed by value (so that if you
change the value of the argument within the function, it does not get
changed outside of the function). If you wish to allow a function to
modify its arguments, you must pass them by reference.
===
and then explain it again why the problem described above is not a bug
although it does not do what PHP documentation states it does.
------------------------------------------------------------------------
[2006-10-01 22:34:35] judas dot iscariote at gmail dot com
again, THIS IS NOT A BUG ¡!
but yes,this behaviuor should be described in the documentation I
think.
I still think this kind of code should raise an E_WARNING or something
( the idea was proposed a while ago, but refused)
------------------------------------------------------------------------
[2006-10-01 22:12:20] plyrvt at mail dot ru
This bug can be described shortly:
"Existance of a reference to the element of an array prevents this
element to be passed by value as long as any reference point to it"
------------------------------------------------------------------------
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/39011
--
Edit this bug report at http://bugs.php.net/?id=39011&edit=1