TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   pass by reference (http://www.talkphp.com/absolute-beginners/4242-pass-reference.html)

allworknoplay 05-06-2009 08:29 PM

pass by reference
 
I know I know...I keep digging up old articles/tutorials and threads and asking about them, but just when I thought I understood pass by ref, I found Wildhoney's article and it confused me somewhat...

His link:

http://www.talkphp.com/vbarticles.ph...s-as-reference



Anyways, I understand what is happening below:

PHP Code:

$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2


$szVar1 is NOT being assigned the value of $szVar2, it is simply being redirected or pointed to its value, which is 'WiredFlame.com'.

Print this out and you get:

szVar1 is: WiredFlame.com
szVar2 is: WiredFlame.com


Now.......

PHP Code:

$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2;
$szVar1 'TalkPHP.com'

Print this out and you get:

szVar1 is: TalkPHP.com
szVar2 is: TalkPHP.com



Could someone please explain to me, how is it that simply passing by reference through $szVar2 changes it? I thought it was a one-way street....

$szVar1 --> I want YOUR value --> $szVar2

$szVar2 (OK) --> Here, I point you to my value --> $szVar1 (thanks)

I don't understand how you can "control" $szVar2 just because you are passing by reference through it...

My logical thinking (which is wrong) would be that $szVar1 would be: TalkPHP.com

But it would leave $szVar2 untouched, and still assigned the value: WiredFlame.com


Also, what is the better practice of using the "&" sign?

$szVar1 =& $szVar2;

or

$szVar1 = &$szVar2;

Village Idiot 05-06-2009 08:51 PM

This is how it is supposed to behave, I'll elaborate when I get home from work.

allworknoplay 05-06-2009 09:13 PM

Quote:

Originally Posted by Village Idiot (Post 23735)
This is how it is supposed to behave, I'll elaborate when I get home from work.

okie dokie, looking forward to it!

Wildhoney 05-06-2009 09:22 PM

I'll let Village Idiot take the lead on this :-) !

Village Idiot 05-06-2009 11:31 PM

Short answer:
Nothing besides the variable we are pointing to has changed, your "assigned by reference" variable is doing its job and referring you to your other variable, no matter what the content may be.

Really long answer:
I have been itching to write something on this for ages now, so here it goes:

Passing by reference is using what is called “Pointers” in more advanced languages. I am not familiar with what PHP lets you do with pointers, so I won’t go too far in depth with anything. To understand the concept of pointers, you have to be familiar with how variables and their values work behind the scene.

How Variables Work
To start, I have to make sure we are all past a common misconception. Most people think that variables are values; they are not. All values are kept in the computers RAM where the variables point to. These attributes are normally hidden to the user, so when you assign a variable you actually modify a value in the ram.

How RAM Works
This is where the computer stores temporary data for fast access and modification. You can look at ram as a grid, every byte has a given spot and each spot has a unique identifier that refers to it. Instead of counting rows and columns on a base ten system (1,2,3,4,5,6,7,8,9,10,11…..), computers count ram spots using a base-16 number system*, so the combination of letters and numbers you may see are actually numbers on a different counting system.

Here is what a section of ram could look like:

As you can see, each address holds a single byte, that byte translates to data that we use.


How this ties together:
Suppose you have the following section of code:
PHP Code:

  $var 10

The first thing the processor does is assign a portion in memory to the given value. So a particular section of your ram could look like


The computer then assigns the address that $var is pointing at to “a020c290”. This is completely transparent to you, every time you use $var you access that address in memory and output 10, the computer takes care of this for you.

Enter pointers:
Since these variables that you are using merely point to memory, why can’t you have a variable whose value (in memory) is another memory address? The answer is that you can, they are called pointers. Instead of being meaningful data, these variables contain data that redirects (or points) the computer to another address in memory.

So suppose you have the following code:
PHP Code:

  $var1 1;
  
$var 2 = &$var1

The computer sets the following in memory (italicized represents a memory address value, the system uses a complicated system to discern between an address and value)


Notice that the value of memory address 2 is the address of memory slot 1. This points whatever is going on to memory address 1. Therefore if you do the following
PHP Code:

  $var1 5

The memory will be changed to:


This changes the value of memory address 1, but leaves memory address 2 pointing to memory address 1. This means that next time you call memory address 2 (the pointer), you will be redirected to address 1, making it appear that the value of the pointer has changed.


*The first 30 number of a hexadecimal (base 16) number system (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,10,11,12,13,14,15 ,16,17,18,19,1a,1b,1c,1d)

Village Idiot 05-06-2009 11:48 PM

If any of this did not make sense, please do tell me so I can clarify.

allworknoplay 05-06-2009 11:52 PM

Quote:

Originally Posted by Village Idiot (Post 23746)
If any of this did not make sense, please do tell me so I can clarify.


Holy cow...I need to print this out and read it!

Thanks...I will let you know shortly if I get or don't get it...

Wasn't your other name on that other board Village Guru?

Village Idiot 05-06-2009 11:53 PM

Quote:

Originally Posted by allworknoplay (Post 23748)
Wasn't your other name on that other board Village Guru?

If you mean Village Genius, that is my screen name on Talkfreelance.com

allworknoplay 05-07-2009 12:05 AM

Quote:

Originally Posted by Village Idiot (Post 23750)
If you mean Village Genius, that is my screen name on Talkfreelance.com

Yup that's it....

Reading on pointers now....


I'll be done in a few, I think you should update your article with this latest info....

Village Idiot 05-07-2009 12:06 AM

Quote:

Originally Posted by allworknoplay (Post 23757)
I'll be done in a few, I think you should update your article with this latest info....

Not sure what you mean by that.

allworknoplay 05-07-2009 12:12 AM

Quote:

Originally Posted by Village Idiot (Post 23758)
Not sure what you mean by that.


Sorry, I'm getting all confused reading these articles.

This was WH's article....

allworknoplay 05-07-2009 12:29 AM

Ok, I read it and I believe I do get it, but it seems to explain one scenario.


PHP Code:

$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2

Output:

szVar1 is: WiredFlame.com
szVar2 is: WiredFlame.com



But according to what WH said, if you change $szVar1 after the fact, like this:

PHP Code:

$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2;  
$szVar1 'TalkPHP.com'

You get this:


szVar1 is: TalkPHP.com
szVar2 is: TalkPHP.com



So I can see how if Var1 is referencing Var2, and you change Var2, and you print Var1, it will reveal the new value that was set on Var2...

I just don't understand why in the second code, how is it possible for Var1 to reference through Var2, then you change Var1 in the last line, and then Var2 changes!!!

Village Idiot 05-07-2009 12:33 AM

Var2 never changes, it is redirecting you to the memory address of var1, thus giving the appearance of it changing value.

allworknoplay 05-07-2009 12:59 AM

Quote:

Originally Posted by Village Idiot (Post 23769)
Var2 never changes, it is redirecting you to the memory address of var1, thus giving the appearance of it changing value.

hmmm.....let me ponder on that one for a second.....

allworknoplay 05-07-2009 01:17 AM

PHP Code:

<?php

//VAR200 LOADED BEFORE ANYTHING ELSE

$var200 'WiredFlame.com';

//VAR100 USING PASS-BY-REFERENCE TO ACCESS $var200 value.

$var100 =& $var200;

echo 
"Var100 is: $var100 <br />";
echo 
"Var200 is: $var200 <br />";

//NOW WE CHANGE THE VALUE OF VAR100

$var100 'TalkPHP.com'


echo 
"Var100 is now: $var100 <br />";
echo 
"Var200 is now: $var200 <br />";


?>

Output:

Var100 is: WiredFlame.com
Var200 is: WiredFlame.com


Var100 is now: TalkPHP.com
Var200 is now: TalkPHP.com


I'm just going to have to accept that this works the way it does. I know I pretty much understand your explanation with the way RAM stores the values.

Something just isn't clicking for me...I can go on and just let it be...but my OCD might kick in where it starts to bother me...

LOL...

allworknoplay 05-07-2009 01:34 AM

It just seems like a violation....

Let me turn this into a skit...


$var100 - "Hey $var200, what is your value....(pass by ref)".

$var200 - "I am valued at 10, so I am redirecting you to where 10 is stored in RAM".


$var100 - "Hey cool thanks man, I'm 10 too now! Not by value, but my reference..."


$var200 - "Cool..."

$var100 - "Oh, I am going to be 20 now...I don't like 10...guess what, now you have to be 20 too now!!"


$var200 - "What? That's not fair, how so? I only redirected you to 10, why do I have to be 20 also? My VALUE is 10...you don't have the rights to change me to 20...what gives?"

Wildhoney 05-07-2009 02:54 AM

15 brains submerged in saline fluid, all linked up to one brain, also submerged in saline fluid so as not to feel left out, in the centre of the room. Happy brains, no concerns. You can imagine that?

Your naive self wanders in and questions brain one.

Naive Self: What do you know, Mr. Brain number 1?
Mr. Brain 1: I know everything every other brain knows.
Naive Self: That is curious!

You move onto the next brain in the line, not believing the nonsense brain number one is telling you.

Naive Self: Please Mr. Brain 2, if you will, tell me what colour the sky is?
Mr. Brain 2: Why of course, dear child! It is green. Don't you know?
Naive Self: Oh, I do know! However, that is wrong.

After questioning 2 brains you're becoming frustrated. You move onto the third brain the line.

Naive Self: Please, please. Do not play with me! What colour is the sky, if I may inquire?
Mr. Brain 3: It is green, of course! Has anybody told you any differently?
Naive Self: You all tell me the same. You're all wrong. Why do you tell me lies?
Mr. Brain 3: We do no such thing, tactless child! You see, we are all linked to the brain in the centre of the room. Do you see it? Well, if you ask the same question, you get the same response. That is because our information is stored there in that brain, not here locally, what you see.
Enlightened Self: Oh my! Muchas gracias, mi amigo!

:-) ! I thought I would carry on the skit there. See, if you ask the same question, because in a sense if $szVar1 is referenced to $szVar2 then they're accessing the same information. The information exists only once, and 2 or more variables access that.

If the brain in the centre of the room says the sky is green, and all subordinate brains are referencing that one single point, then the answer will always be green. If somebody tells the central brain the sky is now orange, all child brains will consider that so.



The child brains hold no data, they are gateways to what's in the mind of the parent. If you change the parent via the child (the gateway) then each child takes on that new information also. It is a consensus.

allworknoplay 05-07-2009 03:12 AM

Leave it to WH to take my skit to another level! LOL...good stuff...

Ok. so basically..the Borg...resistance is futile...






I completely understand now thanks to WH and VI!

I was borderline understanding it, but the "skits" made it go over the top. I completely get it....this is what I was waiting for, something to just click....

Just to explain, I had a completely different viewpoint of what "pass-by-reference" worked...

Say you have $var2 =& $var1;


I thought that $var2 didn't OWN anything. That $var1 provided a "looking" glass to what it's value is, so say it's value is: Blue.

$var2 would just say, "I am blue too, THROUGH $var1"...


But in fact, the pass-by-ref is actually providing the location of the value in RAM (thanks VI) in which $var2 now can dip it's fingers in it and also OWN the value.

This is different from pass-by-value in which $var2 would get a COPY of the value in a different location in RAM. So changing it's value would not affect $var1.

But now that they are in-sync due to pass-by-ref, they are all one collective Borg's!!!!


Did I get it right??

Village Idiot 05-07-2009 03:52 AM

Very good way of explaining it WH. I'll write some more on the practical uses of pointers later.

sketchMedia 05-07-2009 10:19 AM

I was under the impression that PHP's references functioned more like an alias (like a reference in C++) rather than a pointer, in other words they don't point to a memory location but are an alternative name for the target (think of it as a symlink), they more or less achieve the same but are different in terms of what you can do with them, you cant re-seat a reference as you can a pointer (in C++ anyway), you can't have a null reference whereas you can have a null pointer also you can't perform pointer arithmetic on references (which some would consider to be a good thing).

In terms of PHP however, you won't really need an in-depth knowledge of the differences between the too, they will both provide more or less the same functionality (in any case you can't directly fiddle with the memory in PHP like you can in C as its interpreted not compiled).

Just for the sake of illustration, heres a C++ pointer:
c++ Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned short int age = 21;
    unsigned short int * pAge = 0;
    pAge =& age;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;

    *pAge = 25;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;
   
    age = 22;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;

    return 0;
}

and a C++ reference:
c++ Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned short int age = 21;
    unsigned short int&  rAge = age;
   
    cout << "Age: " << age << "ref age: " << rAge << endl;
   
    age = 25;

    cout << "Age: " << age << "ref age: " << rAge << endl;

    rAge = 22;

    cout << "Age: " << age << "ref age: " << rAge << endl;
   
    return 0;
}


All times are GMT. The time now is 05:37 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0