RE: [PHP] References

From: Date: Tue, 07 Nov 2000 20:51:29 +0000
Subject: RE: [PHP] References
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-24185@lists.php.net to get a copy of this message
There's nothing to stop you returning result directly, so: $ref = &$result; return $ref; becomes return $result; With regard to references in functions, this is a matter of scope. A parser (or compiler) will create memory space for any variables local to a function. However, this memory space only exists for the duration of the function. Once the function terminates the parser releases the memory meaning that any references to it are no longer valid. This allocation of memory occurs on what is called the Heap. Consider the Heap to be a huge block of unused memory available for the computer to use at it pleases. Thus you cannot return the memory address of a variable. However, you can alter the contents of variables within another function by passing them by reference to the function. If you pass variables to a function as a parameter then it is store on the Stack. Each call to a function gets it's own stack space (think about the stack growing upwards like a .... stack). When a parser trys to resolve a variable name it will look in an internal lookup table that will direct it to the Heap or the stack. When looking for items on the stack the parser starts at the top and then works it's way down until it reaches the first variable that matches the name. The stack allows the same variable name to be used in each function section of the stack. Basically the rule of thumb for resolution of variables is first the Heap and then the first occurence on the stack. Errm, I'm going a bit off topic now. I'd suggest looking for a good reference that discusses Scope, the Call Stack and Heap allocation. Enjoy... Neil -----Original Message----- From: Vidyut Luther [mailto:vluther@phpcult.com] Sent: 07 November 2000 19:44 To: php-general@lists.php.net Subject: [PHP] References I was wondering why this is not possible. function get_a_lot_of_data() { $sql = "select * from hugetable"; $result = mysql_query($sql); return &result; } I have to do this all the time, $ref = &$result; return $ref; why do i have to create the reference first, and then pass it ? i have no formal education in programming.. so any of you computer sci majors wanna clear this up for me ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#24185) next »