for anyone who was wondering what "&" meant when used in a function/etc. i wrote this little explination on them (tutorial? whatever you want to call it) to help you better understand those confusing little things.
[[ post version ]]
<?
//references 101 - dta (rob@zerobased.net)
/*
in short [
a reference/pointer REFERS/POINTS to a memory address.
a normal assignment $a=$b just stores the VALUE as its own.
hence:
a=&b ~ $a stores the memory location of $b , so they basically share the same memory slot
if you change one, the other changes
whereas:
a=b ~ $a sets the value of $b in its OWN memory slot. so if the value of b changes after this
line, $a will still be set to its old value.
]
[[with functions]]
1.) one use of references: turn a function into a 'sub' (think vb type sub)
meaning instead of $newVal = function($oldVal,$params) it would
just be function($newVal,$params) and then within the function
$newVal would change itself. sometimes this serves more of a
purpose and can save you memory (by not declaring a new var)
example of 1.1 (the $newVal=function(....) ) way:
(note:this is the 'common' use of a function)
*/
// function to add X to an (int)$oldVal
echo "<b>Example 1.1 (add X to (int) w/o reference)</b><hr>";
$oldVal=5;
function addX($int,$x){
echo "function call: $int+$x<br>";
return $int+$x;
}
$newVal=addX($oldVal,10);
//you expect 15 right?
echo "newVal=" . $newVal . "<br>";
//correctamundo.
// example 1.2 add X using a reference:
echo "<br><b>Example 1.2 (add X to (int) with reference)</b><hr>";
$oldVal=5;
function addXRef(&$int,$x){
echo "function call: $int+$x<br>";
echo "*using reference*<br>";
/* this is where we add $x to $int, but remember
that $int is referenced to $oldVal (using &) so
anything we do to $int happens to $oldVal
once we call addXRef($oldVal,100);
*/
$int+=$x;
/* since $int changed, $oldVal changed too, since
the & sign makes them basically the same thing*/
// also note that using 'return' is optional
}
addXRef($oldVal,100);
// you expect 105 right?
echo "newVal=" . $oldVal . "<br>\n";
/* right, why? because when we called addXRef($oldVal,100) we told
the function that $oldVal=$int and by using a reference (&$int) we told
the function that $int=$oldVal also. try using this function without the
&.. not the same at all huh? why is that? without the reference telling the
function to set $oldVal to $int, the function simply makes $int=$int + $x..
but what good does $int do us while local inside the function?? */
/* 2.)
with strings:
if you still don't grasp the concept of the reference/pointer by now just think of it as
a string(read: twine, shoelace,etc - not var type) holding 2 variables together. its almost
like reverse-assignment -- (meaning: you know that $b=$a means that $a == whatever $b is right??,
well what i mean by reverse-assignment is that you assume whatever $a is, $b will be, but
whatever $b becomes $a won't change.. the reference changes that. it makes it so that when
you change $b - $a will change.. and when you change $a, $b will change)
confusing right?
its annoying in a regex kind of way- they make your head hurt
when you first learn them, but once you find a use they serve
their purpose damn well :)
*/
echo "<br><b>Example 2.1 string references</b><hr>";
$a="value a"; // simple assignments
$b="value b";
echo "a=$a, b=$b<br>";
echo "now \$a=&\$b<br>";
$a=&$b; // a=b and b=a
echo "suppose \$b=\"blah\"<br>";
$b="blah"; // a=b, b=a therefore a="blah" also.
//$a="blah"; same results.. think "reverse-assignment"
echo "now a=$a, b=$b<br>";
echo "<br>throwing in another variable (c)<br>";
// now to throw in another variable.
$c=$b; // c=b b=a a=b .. therefore c=a? nope
/* but why?
because C is not a reference, it does not change when the others do.
it holds the actual VALUE of b, not the memory spot of $b (like $a)
*/
$a="new a";
echo "a=$a , b=$b, c=$c<br>";
// play around with these lines if you are
// confused
echo "<br><b>Example 2.2 array references</b><hr>";
// same as strings, no explination needed really
$arr1[0]="First Array"; // set value
$arr2[0]="static"; //ditto
$arr2[1]="Second Array";
// stock values
echo "arr1[0]=" . $arr1[0] . "<br>";
echo "arr2[0]=" . $arr2[0] . "<br>";
echo "arr2[1]=" . $arr2[1] . "<br>";
echo "<br>reference arr2[1] -> arr1[0]<br>";
$arr2[1]=&$arr1[0];
// values after reference point
echo "arr1[0]=" . $arr1[0] . "<br>";
echo "arr2[0]=" . $arr2[0] . "<br>";
echo "arr2[1]=" . $arr2[1] . "<br>";
// now throw store the value from $arr1[0] in $arr2[0]
// but then change $arr1[0] so $arr2[1] will change..
// do you think that $arr2[0] will ALSO change?
$arr2[0]=$arr1[0]; // copy value
$arr1[0]="New Value";
// values after copy/re-assignment
echo "<br>do some copying arr2[0] -> arr1[0]<br>";
echo "arr1[0]=" . $arr1[0] . "<br>";
echo "arr2[0]=" . $arr2[0] . "<br>";
echo "arr2[1]=" . $arr2[1] . "<br>";
/* if you understand this simple 'explination' (not sure what else to call it) then
i'm sure you can find a use for references.
i hope that cleared up a lot of confusion about them..
feel free to modify/print and eat/improve/etc.
-rob
*/
?>
[[ .php format ]]