note 40307 added to language.references.arent

From: Date: Sat, 28 Feb 2004 06:47:46 +0000
Subject: note 40307 added to language.references.arent
Groups: php.notes 
Request: Send a blank email to php-notes+get-65918@lists.php.net to get a copy of this message
The assertion, "references are not like pointers," is a bit confusing. In the example, the author shows how assigning a reference to a formal parameter that is also a reference does not affect the value of the actual parameter. This is exactly how pointers behave in C. The only difference is that, in PHP, you don't have to dereference the pointer to get at the value. -+-+- int bar = 99; void foo(int* a) { a = &bar; } int main() { int baz = 1; foo(&baz); printf("%d\n", baz); return 0; } -+-+- The output will be 1, because foo does not assign a value to the dereferenced formal parameter. Instead, it reassigns the formal parameter within foo's scope. Alternatively, -+-+- int bar = 99; void foo(int* a) { *a = bar; } int main() { int baz = 1; foo(&baz); printf("%d\n", baz); return 0; } -+-+- The output will be 9, because foo dereferenced the formal parameter before assignment. So, while there are differences in syntax, PHP references really are very much like pointers in C. I would agree that PHP references are very different from Java references, as Java does not have any mechanism to assign a value to a reference in such a way that it modifies the actual parameter's value. ---- Manual Page -- http://www.php.net/manual/en/language.references.arent.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40307 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40307&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40307&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65918) next »