Pointers and references: Difference between revisions

Line 564:
Actual Reference or Pointer "objects" don't exist in PHP, but you can simply tie any given variable or array value together with the "=&" operator. Adding the "&" symbol before a function name and the times a function is called causes the functions return values to be returned by reference. Adding the "&" synbol before any function parameter, and that parameter is passed by reference into the function.
 
As an additional note, the "global" keyword, simply references the variable from the $_GLOBALs_GLOBALS superglobal array into a variable by the same name in the current scope. This is an important distinction as other functions that you call can actually re-reference the $_GLOBALS value to a different variable, and your function which used the "global" keyword is no longer actually linked to anything in the $_GLOBALS array.
 
<lang php><?php
Line 594:
 
?></lang>
 
In most cases, PHP "does the right thing" as pertaining to variables and not duplicating variables in memory that have not been edited. Internally, as variables are copied from one to another, passed in and out of functions, etc, they are Not actually duplicated in memory, but rather referenced until one of these copies is changed (called "copy on write", see debug_zval_dump()). So, in the above functions example, if we took out all the ampersands, it would all work perfectly and not duplicate the big string in memory up until the line where we concatenate and add "EDIT" to the end.
 
===See Also===
* [http://php.net/manual/en/language.references.php php.net:References Explained]
* [http://php.net/manual/en/function.debug-zval-dump.php php.net:debug_zval_dump]
 
=={{header|PicoLisp}}==
Anonymous user