note 33412 added to language.variables

From: Date: Wed, 25 Jun 2003 10:10:15 +0000
Subject: note 33412 added to language.variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-50980@lists.php.net to get a copy of this message
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? ---- Manual Page -- http://www.php.net/manual/en/language.variables.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33412 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33412&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33412&report=yes

« previous php.notes (#50980) next »