:o Hi… I need a simple explanation regarding the &$i … or in other words the difference between passing by value and by reference in php…
i’m taking a course in web design using amp
ajax / mySQL and php…
my problem is more esoteric … i can’t get my head around what passing by reference actually does…
thus
$i = 1; //this is a reference naming a variable and assigning it the value of 1
$i = &1; // so i am still assigning a value to $i but now i am also assigning a reference to the variable…
I need a simple analogy that might help me work this out…
I have 5% quiz today and I need some sort of jog to get my noodle around the difference…
thanks
Terrance
Hi Terrance,
Not sure this will clear things up, but here goes:
In most cases, references are used when passing an array or object into and returning from a function. Passing a reference does not require duplicating the object or array, reducing memory usage and processing time.
For example, lets say you are working with a large image (or other object). Lets say the object is 40mb and needs to be processed by several functions. If you pass the object normally, each function has to create its own copy of the object, eating up huge chunks of memory and taking the time to do so. After processing the object, the function passes it back and the time is again taken to copy the entire modified object back.
Now consider passing a reference to the object. In this case, you are effectively passing the original object back and forth, without copying it and without eating up memory.
For small, non-object simple variables and arrays, this is almost always unnecessary and can make the code harder to debug. Some will argue that it “simplifies” the code, this depends on your point of view.
One thing to note is your example is not quite correct. You would not set $i = &1; … The digit 1 is not a variable or object, it is a literal value and, as such, does not have a handle.
This is a better example:[php]$i = 1;
$j = &i;
$j = $j + 1;
echo $i;[/php]The output will be 2
Now this is a really silly example! Here is a slightly more logical example:[php]function doubleIt(&$num)
{
$num = $num + $num;
}
$i = 2;
doubleIt($i);
echo $i;[/php]The output will be 4
Without using a reference, this same code would look like this:[php]<?php
function doubleIt($num)
{
$num = $num + $num;
return $num;
}
$i = 2;
$i = doubleIt($i);
echo $i;[/php]Again, the output is 4.
You will note that this version requires the use of a return, as well as an assignment ( $i = doubleIt($i); ) in order to work. There is one important difference though, The first version will fail if you do something like this:[php]doubleIt(2);[/php]… because 2 is a literal and does not have a handle. On the other hand, in the second version you could easily do[php]$i = doubleIt(2);[/php]
Sorry, it was too late to edit my post. In looking it over, my first example has a slight error. It should be:[php]$i = 1;
$j = &$i;
$j = $j + 1;
echo $i;[/php]
Sorry about that!
To explain it a slightly different way.
Lets say that your test today is open book and that you can bring your notes from the last lecture to help you but unfortunately you were gone that day. Jim was there though, and he took awesome notes. Better yet, Jim takes the test at a different time and offers you the use of his notes. Jim also asks if you would add a few notes that you had previously taken when he was absent, so that his notebook is completely updated.
The analogy in this situation to passing a reference would be that Jim gives you his notes, you add the notes he missed and use the notebook for the test. After class, you give the updated notebook back to him and he uses it for his test.
The analogy to passing the variable would be Jim gives you the notes, you rewrite them all in your own notebook, which you then use for the test. You also give Jim a list of the missing notes and he rewrites them all in his book.
In the above situation, it may make sense for you both to have your own notebook for future reference, but in many programming scenarios, it doesn’t. The size of the notebook has a lot to do with which option makes more sense. This scenario is admittedly overly simplistic, but it does help illustrate the concept of passing a reference.
Hi Thanks much for taking the time…actually our tests are on line / 15 minutes 5 questions worth 5% each time…
I get it thanks for the example
passing by reference assigns the value of one variable to another
$i = $j;
assigns $j to $i
but
$i = &$j; // references two numbers for future use… thereby removing the additional coding that might be required to upgrade $j vis a vis $i
I think the problem is it’s my first foray to server side …
have been designing wordpress websites for 7 years and realized that i have no idea what makes the
design work…
I know that people turn the light on every day without thinking about why the light comes on…
but once you watch that bulb come on?? and
wonder what the hell happened to make it so??
you won’t ever look at a light switch the same again…
thanks again
terrance