|
Posted by frizzle on 03/18/06 12:47
ColdShine wrote:
> frizzle in news:1142637987.677378.96420@z34g2000cwc.googlegroups.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.
Navigation:
[Reply to this message]
|