ampersand before var in function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frizzle

    #1

    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.

  • noone

    #2
    Re: ampersand before var in function

    frizzle wrote:[color=blue]
    > 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.
    >[/color]


    from:


    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.

    Comment

    • frizzle

      #3
      Re: ampersand before var in function


      noone wrote:[color=blue]
      > frizzle wrote:[color=green]
      > > 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.
      > >[/color]
      >
      >
      > 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.[/color]

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

      Thanks.

      Frizzle.

      Comment

      • ColdShine

        #4
        Re: ampersand before var in function

        frizzle in news:1142637987 .677378.96420@z 34g2000cwc.goog legroups.com wrote:
        [color=blue]
        > noone wrote:
        >[color=green]
        >> frizzle wrote:[/color]
        >[color=green][color=darkred]
        >>> 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 ...[/color]
        >>
        >> 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.[/color]
        >
        > Hmm, could you maybe give me a small simple example ?[/color]

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

        $arg1 = 1;
        $arg2 = 2;
        modify_arg1($ar g1, $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


        Comment

        • frizzle

          #5
          Re: ampersand before var in function


          ColdShine wrote:[color=blue]
          > frizzle in news:1142637987 .677378.96420@z 34g2000cwc.goog legroups.com wrote:
          >[color=green]
          > > noone wrote:
          > >[color=darkred]
          > >> frizzle wrote:[/color]
          > >[color=darkred]
          > >>> 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.[/color]
          > >
          > > Hmm, could you maybe give me a small simple example ?[/color]
          >
          > function modify_arg1(&$a rg1, $arg2)
          > {
          > $arg1 = 'first';
          > $arg2 = 'second';
          > }
          >
          > $arg1 = 1;
          > $arg2 = 2;
          > modify_arg1($ar g1, $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[/color]

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

          Frizzle.

          Comment

          Working...