what does the & in some php meen?

Posted on 2006-05-16
Last Modified: 2012-05-05

I see in php lines sometimes & and it appears to not meen AND? Can someone emplain this to me please?

$this->connection =& Database::getInstance();

Question by:NeilT
    LVL 49

    Accepted Solution


    This means you are retrieving by reference, rather than creating a new instance and copying its parameters.

    It is best explained in the manual actually:, from which I copied and pasted below texts:

    [What References Are]
    References in PHP are a means to access the same variable content by different names. They are not like C pointers; instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem.

    [What References Do]
    PHP references allow you to make two variables to refer to the same content. Meaning, when you do:

    $a =& $b;

    it means that $a and $b point to the same content.
    Note: $a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place.

    Note: If array with references is copied, its values are not dereferenced. This is valid also for arrays passed by value to functions.

    The same syntax can be used with functions, that return references, and with new operator (in PHP 4.0.4 and later):

    $bar =& new fooclass();
    $foo =& find_var($bar);

    Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message.

    Hope this helps. You find more info in the actual manual

    LVL 3

    Author Comment

    thanks Roonaan

    I'm 75% understanding it now so I'll have a good read up now I know the 'concepts'

    Many thanks
    LVL 40

    Assisted Solution

    In addition to Roonaan's comments, I would add that the & can also be used to supply what is sometimes known as output parameters for functions.

    A common problem is you have a function x() which can return more than 1 value.

    There are many solutions to this, but a common one is to use output parameters.


    function x($i_input_param, &$i_output_param1, &$i_output_param2      )
     $i_output_param1 = $i_input_param * 10;
     $i_output_param2 = $i_input_param * 100;
     return True;

    if (True === x(1, $i_10, $i_100))
     echo "\$i_10 = $i_10 and \$i_100 = $i_100";

    results in ...

    $i_10 = 10 and $i_100 = 100

    There are many PHP functions which use output parameters, though for some reason, I can only think of the PREG functions.

    int preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )

    This ability to pass data back out of a function without resorting to knowing the external variable name for a GLOBAL or returning an array which the caller has to parse is a clean way of writing modular code.
    LVL 3

    Author Comment

    Many thanks

    I can see the uses for transparency more now and can see the usefulness in creating modular code.

    great answers, very consise, just what I needed.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now