What's the & sign use for?
Moderator: General Moderators
What's the & sign use for?
Hi there,
Just walking in the tutor section and found a syntax which is not familiar with a "&" infront of a variable. Anybody know what's this use for? Any simple code for description is much appreciated.
TQ.
Best Regards,
Nelson Neoh
Just walking in the tutor section and found a syntax which is not familiar with a "&" infront of a variable. Anybody know what's this use for? Any simple code for description is much appreciated.
TQ.
Best Regards,
Nelson Neoh
I believe it means to work on a reference of the variable, or to pass the variable by reference.
http://us3.php.net/manual/en/language.references.php
http://us3.php.net/manual/en/language.references.php
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Thanks guys.
A simple try for my understanding:
Code: Select all
$a = 10;
$c = &$a;
$d = $a++;
echo $c; // <-- display 10?
echo $d; // <-- display 11?- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Code: Select all
$a = 10;
$b = $a;
$c = &$a;If you use the & symbol as in $c= &$a a new $c variable is created but it points to the $a variable instead of creating a duplicate variable. If you alter the content of $c you WILL alter the content of $a.
This doesn't really save any speed or memory if you are using numeric (integer, float, etc) variables. What it really saves memory on is classes, large arrays and string variables.
If you have a string variable with 50,000 characters in it you will save a nice chunk of memory if you reference the variable instead of just copying it to another variable name.
There is one caveat to using references.
If you change what is in the referenced variable $c above to $c = 999; you will be changing the $a variable as well and the reverse is true. So be careful about using references. If you reference one variable a dozen times and change the content of any of the reference variables you change the content in ALL of the referenced variables.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Not sure if AKAPJ mentions this or not or if it's even relevent, but...
That operator has dual meaning in PHP and depends on it's context...It's also the bitwise AND operator.
Whenever you change $foo or $boo, the change is applied to both variables, because $foo is a reference of $boo.
It's not a copy, but a reference. Normally if you used the above code *without* the & operator $foo would become a "copy" of the $boo variable, because you basically assign the contents of $boo to $foo. However using the & operator the assignment now takes a slightly different role, in that it doesn't copy the "content" but rather the location or memory address of $boo into the reference variable $foo.
Basically $foo has become a special kind of variable called a reference. You will often hear people use words like:
Copy by reference
Copy by value
The latter is what happens *without* the & operator and the former is what happens when you make the assigned variable a reference variable.
Consider the following:
The first line the variable $foo is assigned the "content" Hello World and it starts at the memory address 1200 and takes up 12 bytes.
The first variable $boo is a copy of $foo and therefore also takes up 12 bytes. togather these two variables take up 24 bytes (could be more or less but thats not what matters)
The second variable $boo is a special variable called a reference. It doesn't copy the 'contents' of $foo but instead copies the 'address' of $foo so this instance of $boo doesn't take up any memory (except for itself - but again irrelevant).
It simply 'points' to the memory location of the variable it references. Thats why it's 4 digit address inside the brackets is the same as the original $foo.
Secondly, the & operator has the meaning of bitwise AND operator...it can be used to check whether bits in a variable are set. For instance:
After the above operation $var would contain 0 because the first bit is not set.
That operator has dual meaning in PHP and depends on it's context...It's also the bitwise AND operator.
Code: Select all
$boo = 'This is a string';
$foo = &$boo; // $foo now points too $booIt's not a copy, but a reference. Normally if you used the above code *without* the & operator $foo would become a "copy" of the $boo variable, because you basically assign the contents of $boo to $foo. However using the & operator the assignment now takes a slightly different role, in that it doesn't copy the "content" but rather the location or memory address of $boo into the reference variable $foo.
Basically $foo has become a special kind of variable called a reference. You will often hear people use words like:
Copy by reference
Copy by value
The latter is what happens *without* the & operator and the former is what happens when you make the assigned variable a reference variable.
Consider the following:
The brackets with the 4 digits are random memory addresses PHP assigns each variable.(1200)$foo = 'Hello world';
(1212)$boo = $foo;
(1200)$boo = &$foo;
The first line the variable $foo is assigned the "content" Hello World and it starts at the memory address 1200 and takes up 12 bytes.
The first variable $boo is a copy of $foo and therefore also takes up 12 bytes. togather these two variables take up 24 bytes (could be more or less but thats not what matters)
The second variable $boo is a special variable called a reference. It doesn't copy the 'contents' of $foo but instead copies the 'address' of $foo so this instance of $boo doesn't take up any memory (except for itself - but again irrelevant).
It simply 'points' to the memory location of the variable it references. Thats why it's 4 digit address inside the brackets is the same as the original $foo.
Secondly, the & operator has the meaning of bitwise AND operator...it can be used to check whether bits in a variable are set. For instance:
Code: Select all
$var = 10; // Binary = 0000 1010
$var = ($var & 1); // Check if first bit of var is set and assign to itselfCode: Select all
$var = &$var; // Assign a reference to itself (not sure of the results of that :? )
$var = $var & 1; // Test is first bit is setThe above is wrong, the right answer would be this:shneoh wrote:A simple try for my understanding:
Code: Select all
$a = 10; $c = &$a; $d = $a++; echo $c; // <-- display 10? echo $d; // <-- display 11?
Code: Select all
$a = 10;
$c = &$a;
$d = $a++;
echo $c; // <-- display 11?
echo $d; // <-- display 10?I think the best way to understand the referenced variables is this example:
Code: Select all
$a = 10;
$b = &$a;
$b++;
echo $a; // Will display 11, because the $b refers to the same memory cell in which the value of $a is savedThanks guys! Your information even worthy than those documents in PHP.net... 
I really appreciated on your effort and you have truely make myself clear on this "&" usage! Special thanks for AKAPJ and Hocky, your artical is really useful for those not programming newbie but just new to php. It is worthy to move into the newbie forum.
So now I have understand throughly on the reference "&". Thanks again.
As a result:

I really appreciated on your effort and you have truely make myself clear on this "&" usage! Special thanks for AKAPJ and Hocky, your artical is really useful for those not programming newbie but just new to php. It is worthy to move into the newbie forum.
So now I have understand throughly on the reference "&". Thanks again.
As a result:
Code: Select all
$my_understanding = &$AKAPJ.&$Hocky;