What's the & sign use for?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

What's the & sign use for?

Post by shneoh »

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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
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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you use the & to create a reference.

edit: damn it scottay...quick draw mcgraw you are!
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

:roll:

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?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Code: Select all

$a = 10;

$b = $a;

$c = &$a;
In the above example when you have $b = $a php will allocate memory for the new $b variable creating a duplicate variable. If you alter the content of $b the content of $a will stay the same.

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

Post by alex.barylski »

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.

Code: Select all

$boo = 'This is a string';

$foo = &$boo; // $foo now points too $boo
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:
(1200)$foo = 'Hello world';
(1212)$boo = $foo;
(1200)$boo = &$foo;
The brackets with the 4 digits are random memory addresses PHP assigns each variable.

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 itself
After the above operation $var would contain 0 because the first bit is not set.

Code: Select all

$var = &$var; // Assign a reference to itself (not sure of the results of that :? )
$var = $var & 1; // Test is first bit is set
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

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?
The above is wrong, the right answer would be this:

Code: Select all

$a = 10;

$c = &$a;
$d = $a++;

echo $c;  // <-- display 11?
echo $d;  // <-- display 10?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

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 saved
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Think of passing by value as cloning the value, and passing/assigning by reference as giving the same value an extra label.
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

Thanks 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:

Code: Select all

$my_understanding = &$AKAPJ.&$Hocky;
:roll:
Post Reply