note 79086 added to language.references
| From: | warnickr at gmail dot com | Date: | Sat, 10 Nov 2007 09:15:51 +0000 |
| Subject: | note 79086 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132159@lists.php.net to get a copy of this message | ||
I must say that it has been rather confusing following all of the explanations of PHP references,
especially since I've worked a lot with C pointers. As far as I can tell PHP references are
the same as C pointers for all practical purposes. I think a lot of the confusion comes from
examples like the one shown below where people expect that a C pointer version of this would change
what $bar references.
<?php
function foo(&$var)
{
$var =& $GLOBALS["baz"];
}
foo($bar);
?>
This is not the case. In fact, a C pointer version of this example (shown below) would behave
exactly the same way (it would not modify what bar references) as the PHP reference version.
int baz = 5;
int* bar;
void foo(int* var)
{
var = &baz;
}
foo(bar);
In this case, just as in the case of PHP references, the call foo(bar) doesn't change what bar
references. If you wanted to change what bar references, then you would need to work with a double
pointer like so:
int baz = 5;
int* bar;
void foo(int** var)
{
*var = &baz;
}
foo(&bar);
----
Server IP: 69.147.83.197
Probable Submitter: 76.27.53.38
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/79086
Del: integrated -- https://master.php.net/note/delete/79086/integrated
Del: useless -- https://master.php.net/note/delete/79086/useless
Del: bad code -- https://master.php.net/note/delete/79086/bad+code
Del: spam -- https://master.php.net/note/delete/79086/spam
Del: non-english -- https://master.php.net/note/delete/79086/non-english
Del: in docs -- https://master.php.net/note/delete/79086/in+docs
Del: other reasons-- https://master.php.net/note/delete/79086
Reject -- https://master.php.net/note/reject/79086
Search -- https://master.php.net/manage/user-notes.php