470,815 Members | 1,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

ampersand before var in function

Hi there,

Recently i noticed a few times an ampersand in front of a variable in a
function.
Like so:

function foo( &$bar )
{
// do something
};

What is it for? I can't figure it out ...

Frizzle.

Mar 17 '06 #1
4 2597
frizzle wrote:
Hi there,

Recently i noticed a few times an ampersand in front of a variable in a
function.
Like so:

function foo( &$bar )
{
// do something
};

What is it for? I can't figure it out ...

Frizzle.

from:
http://www.softwareprojects.org/php-functions-12.htm

Another way to access the data outside the function, without using an
argument is by using a reference to a variable as an argument. You can
do this by placing an ampersand “&” in front of the argument, when you
define the function. This way, the function will directly access the
variable thru the reference, and the variable can be assigned and read
at the same time.

Mar 17 '06 #2

noone wrote:
frizzle wrote:
Hi there,

Recently i noticed a few times an ampersand in front of a variable in a
function.
Like so:

function foo( &$bar )
{
// do something
};

What is it for? I can't figure it out ...

Frizzle.

from:
http://www.softwareprojects.org/php-functions-12.htm

Another way to access the data outside the function, without using an
argument is by using a reference to a variable as an argument. You can
do this by placing an ampersand "&" in front of the argument, when you
define the function. This way, the function will directly access the
variable thru the reference, and the variable can be assigned and read
at the same time.


Hmm, could you maybe give me a small simple example ?

Thanks.

Frizzle.

Mar 17 '06 #3
frizzle in news:11*********************@z34g2000cwc.googlegro ups.com wrote:
noone wrote:
frizzle wrote:

Hi there,

Recently i noticed a few times an ampersand in front of a variable in a
function.
Like so:

function foo( &$bar )
{
// do something
};

What is it for? I can't figure it out ...


from:
http://www.softwareprojects.org/php-functions-12.htm

Another way to access the data outside the function, without using an
argument is by using a reference to a variable as an argument. You can
do this by placing an ampersand "&" in front of the argument, when you
define the function. This way, the function will directly access the
variable thru the reference, and the variable can be assigned and read
at the same time.


Hmm, could you maybe give me a small simple example ?


function modify_arg1(&$arg1, $arg2)
{
$arg1 = 'first';
$arg2 = 'second';
}

$arg1 = 1;
$arg2 = 2;
modify_arg1($arg1, $arg2);
echo $arg1;
echo $arg2;

Outputs:
first
2

Hope this helps!
--
ColdShine

"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law
Mar 18 '06 #4

ColdShine wrote:
frizzle in news:11*********************@z34g2000cwc.googlegro ups.com wrote:
noone wrote:
frizzle wrote:

Hi there,

Recently i noticed a few times an ampersand in front of a variable in a
function.
Like so:

function foo( &$bar )
{
// do something
};

What is it for? I can't figure it out ...

from:
http://www.softwareprojects.org/php-functions-12.htm

Another way to access the data outside the function, without using an
argument is by using a reference to a variable as an argument. You can
do this by placing an ampersand "&" in front of the argument, when you
define the function. This way, the function will directly access the
variable thru the reference, and the variable can be assigned and read
at the same time.


Hmm, could you maybe give me a small simple example ?


function modify_arg1(&$arg1, $arg2)
{
$arg1 = 'first';
$arg2 = 'second';
}

$arg1 = 1;
$arg2 = 2;
modify_arg1($arg1, $arg2);
echo $arg1;
echo $arg2;

Outputs:
first
2

Hope this helps!
--
ColdShine

"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law


Ha that's great! So it changes a variable that exists before the
function is called!
Cool.

Frizzle.

Mar 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Magnus Warker | last post: by
4 posts views Thread by tfortney | last post: by
9 posts views Thread by keepyourstupidspam | last post: by
3 posts views Thread by jodleren | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.