You are here: Re: [PHP] Function shall receive Pointer to Array « PHP « IT news, forums, messages
Re: [PHP] Function shall receive Pointer to Array

Posted by Richard Lynch on 10/07/05 11:15

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация