note 33412 deleted from language.variables by aidan
| From: | aidan@php.net | Date: | Sat, 18 Sep 2004 04:45:03 +0000 |
| Subject: | note 33412 deleted from language.variables by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76920@lists.php.net to get a copy of this message | ||
Note Submitter: eye of a raven
----
jeeez people, why do you always get confused in the simplest things.
here's how referencing works:
to store any value you need a segment of memory.
internally, in machine code the program is just a sequence of numbers. this is also valid for
interpreted languages since their parsing engines are compiled executables, so they operate on raw
machine code.
so the question is: how does the program 'know' what is what? what programmers call
'variables' are not magical entities in space. they are just places in memory. period.
every place in memory has a (duh...) memory address.
the NAME of a variable is a human-readable identifier so that instead of writing raw memory
addresses we can use fancy names to denote them.
so, in php:
<?
$foo = 5; // "foo" is a name. internally it might be replaced by something like, say
0x3a34b4 which is a memory address in hex notation
$bar; // "bar" is also a name. the parser sees no assignment so "$bar" maps to
NULL (probably 0x00, dunno...)
$fooReference = &$foo; //this is a reference assignment. "fooReference" is a name
that maps to the same thing as "foo".
$barReference = &$bar; //hehe... now what? "barReference" also maps to NULL. but what
if $bar is later changed?
?>
references ARE NOT C pointers! a C pointer is an actual variable that stores memory addresses, just
like any other variable that stores integers or floats or other.
a reference variable is an ALIAS for a persistent and already existing memory address (that's
why there is no copying and that's why you can't assign: $ref = &$(20*4-1) ).
ps. I assume there is another level of indirection between identifiers and actual memory addresses
that lets the engine differentiate between foo and fooReference. if not, i suppose the variable
names can be treated like C macros?