Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of using & before a function's argument?

I saw some function declarations like this:

function boo(&$var){  ... } 

what does the & character do?

like image 930
Alex Avatar asked Nov 26 '10 01:11

Alex


People also ask

What is the purpose of using it?

Information Technology (IT) is used in business for transmitting, storing, manipulating and retrieving data. The purpose of IT used in business are: Information Technology helps to store the information.

What is an example of purpose?

Purpose is defined as to plan or intend to do something. An example of purpose is someone deciding they will start saving 10% of their income. An object to be reached; a target; an aim; a goal. A result that is desired; an intention.

What is the real purpose of life?

All life forms have one essential purpose: survival. This is even more important than reproduction. After all, babies and grannies are alive but don't reproduce. To be alive is more than passing genes along.

What is the purpose of the text?

The purpose of a text is simply the writer's reason for writing. Many texts have more than one purpose, but usually one will stand out as primary. Readers have the job of determining the purpose or purposes of a text and understanding why the writer is writing and what the writer wants the reader to do with the text.


2 Answers

It's a pass by reference. The variable inside the function "points" to the same data as the variable from the calling context.

function foo(&$bar) {   $bar = 1; }  $x = 0; foo($x); echo $x; // 1 
like image 120
Matthew Avatar answered Oct 15 '22 15:10

Matthew


Basically if you change $var inside the function, it gets changed outside. For example:

$var = 2;  function f1(&$param) {     $param = 5; }  echo $var; //outputs 2 f1($var); echo $var; //outputs 5 
like image 38
cambraca Avatar answered Oct 15 '22 14:10

cambraca