Re: Function shall receive Pointer to Array
| From: | Richard Lynch | Date: | Fri, 06 May 2005 05:31:45 +0000 |
| Subject: | Re: Function shall receive Pointer to Array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-214591@lists.php.net to get a copy of this message | ||
On Wed, May 4, 2005 3:29 pm, Fred Rathke said:
> how can a function get a pointer to an array? This does not work. I use
> PHP4.
Technically, in PHP, they are references, not pointers.
There are no pointers in PHP.
The difference is too subtle for me to understand it, but there is
apparently some meaningful difference.
> $t = array("test" => "unchanged");
> echo "<br>testarray
> unchanged:\"".$t['test']."\"";
> changearray($t);
> echo "<br>testarray hopefully
> changed:\"".$t['test']."\"";
>
> function changearray(&$myarray) {
> $myarray['test'] = "changed";
> }
-bash-2.05b$ php -a
Interactive mode enabled
<?php
$t = array("test" => "unchanged");
echo "<br>testarray unchanged:\"".$t['test']."\"";
changearray($t);
echo "<br>testarray hopefully
changed:\"".$t['test']."\"";
function changearray(&$myarray) {
$myarray['test'] = "changed";
}
?>
Content-type: text/html
X-Powered-By: PHP/4.3.11
<br>testarray unchanged:"unchanged"<br>testarray hopefully
changed:"changed"
-bash-2.05b$
So it works in 4.3.11
Exactly which version of PHP are you one -- minor version numbers included.
The behaviour of & changed several times over the course of PHP's history,
which makes meaningful discussion difficult without precision in version
information.
> Before I tried it on my own I read this page:
> http://de2.php.net/manual/en/language.references.whatdo.php
>
> Be so nice to search for this string: "The second thing references do
> is to pass variables by-reference. This is done by making a local
> variable in a function and a variable in the calling scope reference
> to the same content. Example:"
>
> The following example I used. I only tried to do it with an array
> instead of a variable.
Actually, I would expect arrays to *ALWAYS* have worked in PHP...
But maybe that's just my shoddy memory.
Plus I rarely send arrays around in functions and try to alter their
contents. Just a question of programming style, I guess.
> What I need to read again to find my own mistake? I know some of php's
> commands work with an internal copy of a content.
foreach() does that for sure.
--
Like Music?
http://l-i-e.com/artists.htm