[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

what does the & in some php meen?

Hi

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

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

Thanks
Neil
0
Neil Thompson
Asked:
Neil Thompson
  • 2
2 Solutions
 
RoonaanCommented:
Hi,

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: http://nl3.php.net/manual/en/language.references.php, 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:


<?php
$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):

<?php
$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

-r-
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
thanks Roonaan

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

Many thanks
Neil
0
 
Richard QuadlingSenior Software DeverloperCommented:
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.

e.g.

<?php
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.
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
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.

Neil
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now