PHP: What is the & Ampersand Preceding Variables

Well, this one is hard to find on Google, so I’m putting it in WhyPad to make it easy to find…at least for me, and I guess for you too since you here. Welcome! You may see PHP code snippets (PHP 5+ only) that have an ampersand, ‘&’, preceding a variable like &$my_variable. So, what does it do? It sets up a reference to the original variable instead of copying it’s value. The following snippet demonstrates:

$original = "foo";
$ref = &$original;

echo $ref;   \\Prints  "foo"....Note that you don't continue to use the '&' after the var is initialized

now change $original

$original = "bar";

echo $ref;  \\Now prints:  "bar"

$ref would be unaffected by changes to $original if it had been set using the normal: $ref = $original;

Next to figure out what that @ is doing in PHP code… 😉

[UPDATE] David sheds light on the “@” below…Thanks David!


14 Responses to PHP: What is the & Ampersand Preceding Variables

  1. David June 8, 2008 at 7:11 am #

    Hah, thanks for reminding me about what the “&” does. Hadn’t used it in such a long time I’d forgotten.

    As for the “@”, it’s used for operators and functions to tell PHP not report any errors if they should occur. A good example is if you want to delete a file – should that file not exist, PHP would return the error message saying that.

  2. byron June 8, 2008 at 1:01 pm #

    Thanks for dropping by! And thanks for the note on “@”. I keep forgetting these little details since I don’t use them very often.


  3. Jary July 11, 2008 at 10:25 pm #

    hi im using php 5.x and im getting error whenever i try to use &$ref..pls help thanks

  4. byron July 12, 2008 at 10:14 am #

    Hi Jary,

    Here are some alternative methods to try:

    $new_var =& $old_var;

    or if passing through a function

    function my_fun(& $old_var){


    If those don’t work, post your code and I’ll take a look.


  5. Hayden Charles March 16, 2009 at 9:22 am #

    I recently installed PHP5.2.8 using EasyPHP 3.0 on an XP machine and have been trying to install PEAR in order to access its templates but I keep getting a warning that the “include_path defined in the currently used php.ini does not contain the PEAR PHP directory you just specified”.

    However, even though I have changed the include_path in php.ini (which is located at C:\Program Files\EasyPHP 3.0\php) to “c:\Program Files\EasyPHP 3.0\php\pear”, I still keep getting the warning that the “Current include path is .;C:\php5\pear”.

    I have looked all over and have not located the php.ini file on the C: drive with the path .;C:\php5\pear. I cannot imagine where the include path .;C:\php5\pear is being read from during installation.

    I would appreciate it if someone could tell me where this path is being read from and how I can change it.

    I need to access the PEAR templates to build my application.

  6. andy May 23, 2009 at 6:25 pm #

    Nice! You’re right, it was hard to find on Google, excluding this post.

  7. Roel August 27, 2009 at 2:44 am #

    ty, cool read hard to find!

  8. Andrew November 5, 2009 at 4:35 pm #

    Help I’ve got a completely random PHP problem!

  9. Al November 19, 2009 at 11:38 am #

    This is good to know. Google should have a way to find literal code like operators.

    Shouldn’t the line:

    &$ref = $original;

    have the ampersand on the second variable?

    $ref = &$original;

  10. Byron Bennett November 19, 2009 at 11:43 am #


    Yep…you’re right…fixed. Thanks!


  11. ToughT May 9, 2010 at 3:42 pm #

    Hey, thanks for the great tip. I’ve always wondered what the ampersand did. That will definitely be useful! Cheers

  12. Ben June 28, 2010 at 5:50 pm #


    This is a site that lets you query source code of programming books with special characters like ‘&’.

  13. Bradford July 20, 2010 at 1:07 pm #

    Thanks! Your site was the first that came up in my search. This behavior is like in java when a variable simply contains a pointer to another variable such as

    String s = “hello”;
    String t = s;

    t is simply pointing to the memory location of s and when you change the value of s, t points to s all the same and will return the same new value.

  14. gautam October 27, 2010 at 3:14 am #

    exactly what i want thanks a lot

Powered by WordPress. Designed by Woo Themes