Function Parameter passing? Session? Static? Global?

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

    Function Parameter passing? Session? Static? Global?

    Dear Newsgroupers,

    The 'main' page contains a call to a function in an included file. This
    function puts a html-form on the screen. Before the form gets posted (to the
    'main' page) some prior errorchecking is done. The form fields are then sent
    and an object is created (all in the same function).
    What I need ( imo ) is an array with objects that can be written later to a
    dbfile to preserve.

    But I cannot find a solution for the extension of the array.

    the function statements are simply these:

    < inside function in included file >
    $o = someObject( $s ) ;
    $objarr[] = $o;
    </ inside function in included file >

    As this array is within a function it is local to this function.
    As I understand it now a good solution would be to pass the array to the
    function.
    In the 'main' page I made a variable $objarr that is passed into the
    function like this:
    <'main' page>
    $objarr ;
    function( $objarr )
    </'main' page>

    The PHP manual states that in the absence of the array variable it gets
    created. {Does this mean that function( $objarr ) would suffice? }
    However,
    whatever I try, I end up with the creation of $objarr[ 0 ] only.

    Please advise

    TIA,

    pablo


  • Pedro Graca

    #2
    Re: Function Parameter passing? Session? Static? Global?

    pablo wrote:[color=blue]
    > What I need ( imo ) is an array with objects that can be written later to a
    > dbfile to preserve.
    >
    > But I cannot find a solution for the extension of the array.
    >
    > the function statements are simply these:
    >
    > < inside function in included file >
    > $o = someObject( $s ) ;
    > $objarr[] = $o;
    > </ inside function in included file >
    >
    > As this array is within a function it is local to this function.
    > As I understand it now a good solution would be to pass the array to the
    > function.
    > In the 'main' page I made a variable $objarr that is passed into the
    > function like this:
    > <'main' page>
    > $objarr ;
    > function( $objarr )
    > </'main' page>
    >
    > The PHP manual states that in the absence of the array variable it gets
    > created. {Does this mean that function( $objarr ) would suffice? }
    > However,
    > whatever I try, I end up with the creation of $objarr[ 0 ] only.
    >
    > Please advise[/color]


    Pass the function parameter by reference:

    pedro$ cat xx.php
    <?php
    function more_array(&$ar r) {
    // reference _____^_____
    $arr[] = 'one';
    $arr[] = 'two';
    }

    more_array($x);
    print_r($x);
    ?>

    pedro$ php xx.php
    Array
    (
    [0] => one
    [1] => two
    )

    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • pablo

      #3
      Re: Function Parameter passing? Session? Static? Global?


      "Pedro Graca" <hexkid@hotpop. com> wrote in message
      news:slrncctcn1 .mqf.hexkid@ID-203069.user.uni-berlin.de...
      and gave some answer:[color=blue]
      > pablo wrote:[color=green]
      > > What I need ( imo ) is an array with objects that can be written later[/color][/color]
      to a[color=blue][color=green]
      > > dbfile to preserve.
      > >
      > > But I cannot find a solution for the extension of the array.
      > >
      > > the function statements are simply these:
      > >
      > > < inside function in included file >
      > > $o = someObject( $s ) ;
      > > $objarr[] = $o;
      > > </ inside function in included file >
      > >
      > > As this array is within a function it is local to this function.
      > > As I understand it now a good solution would be to pass the array to the
      > > function.
      > > In the 'main' page I made a variable $objarr that is passed into the
      > > function like this:
      > > <'main' page>
      > > $objarr ;
      > > function( $objarr )
      > > </'main' page>
      > >
      > > The PHP manual states that in the absence of the array variable it gets
      > > created. {Does this mean that function( $objarr ) would suffice? }
      > > However,
      > > whatever I try, I end up with the creation of $objarr[ 0 ] only.
      > >
      > > Please advise[/color]
      >
      >
      > Pass the function parameter by reference:
      >
      > pedro$ cat xx.php
      > <?php
      > function more_array(&$ar r) {
      > // reference _____^_____
      > $arr[] = 'one';
      > $arr[] = 'two';
      > }
      >
      > more_array($x);
      > print_r($x);
      > ?>
      >
      > pedro$ php xx.php
      > Array
      > (
      > [0] => one
      > [1] => two
      > )[/color]
      First of all thanks for the answer, but I find it difficult to understand.
      I tried to follow your example but it doesn't solve my problem.
      Your array $x seems to exist already outside the function.
      As I see it I can go about in this way:
      $x[] = "three";
      more_array( $x ) ;

      And I suspect the function is not necessary.

      But what I want to add to the array is contained and created inside the
      function.

      pablo





      Comment

      • Pedro Graca

        #4
        Re: Function Parameter passing? Session? Static? Global?

        pablo wrote:[color=blue]
        >
        > "Pedro Graca" <hexkid@hotpop. com> wrote in message
        > news:slrncctcn1 .mqf.hexkid@ID-203069.user.uni-berlin.de...
        > and gave some answer:[color=green]
        >> pablo wrote:[color=darkred]
        >> > What I need ( imo ) is an array with objects that can be written later[/color][/color]
        > to a[color=green][color=darkred]
        >> > dbfile to preserve.
        >> >
        >> > But I cannot find a solution for the extension of the array.
        >> >
        >> > the function statements are simply these:
        >> >
        >> > < inside function in included file >
        >> > $o = someObject( $s ) ;
        >> > $objarr[] = $o;
        >> > </ inside function in included file >
        >> >
        >> > As this array is within a function it is local to this function.
        >> > As I understand it now a good solution would be to pass the array to the
        >> > function.
        >> > In the 'main' page I made a variable $objarr that is passed into the
        >> > function like this:
        >> > <'main' page>
        >> > $objarr ;
        >> > function( $objarr )
        >> > </'main' page>
        >> >
        >> > The PHP manual states that in the absence of the array variable it gets
        >> > created. {Does this mean that function( $objarr ) would suffice? }
        >> > However,
        >> > whatever I try, I end up with the creation of $objarr[ 0 ] only.
        >> >
        >> > Please advise[/color]
        >>
        >>
        >> Pass the function parameter by reference:
        >>
        >> pedro$ cat xx.php
        >> <?php
        >> function more_array(&$ar r) {
        >> // reference _____^_____
        >> $arr[] = 'one';
        >> $arr[] = 'two';
        >> }
        >>
        >> more_array($x);
        >> print_r($x);
        >> ?>
        >>
        >> pedro$ php xx.php
        >> Array
        >> (
        >> [0] => one
        >> [1] => two
        >> )[/color][/color]
        [color=blue]
        > First of all thanks for the answer, but I find it difficult to understand.
        > I tried to follow your example but it doesn't solve my problem.
        > Your array $x seems to exist already outside the function.[/color]

        It is created right before the more_array() function is called.

        Right before the script gets to the line
        more_array($x);

        $x is undefined.
        For PHP to be able to call the function, because the function expects a
        reference, the parameter *must* exist, so it is created right there.
        [color=blue]
        > As I see it I can go about in this way:
        > $x[] = "three";[/color]

        If $x was undefined before last line, it is now an array with an element
        with index 0 which holds the string "three".
        [color=blue]
        > more_array( $x ) ;[/color]

        Calling print_r($x) now would output
        [0] => three
        [1] => one
        [2] => two
        [color=blue]
        > And I suspect the function is not necessary.[/color]

        ??????
        [color=blue]
        > But what I want to add to the array is contained and created inside the
        > function.[/color]

        Right! That is why you should use references :)
        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


        --
        USENET would be a better place if everybody read: | to email me: use |
        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
        http://www.expita.com/nomime.html | no attachments. |

        Comment

        • pablo

          #5
          Re: Function Parameter passing? Session? Static? Global?


          "Pedro Graca" <hexkid@hotpop. com> wrote in message
          news:slrncctv73 .mqf.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
          > pablo wrote:[color=green]
          > >
          > > "Pedro Graca" <hexkid@hotpop. com> wrote in message
          > > news:slrncctcn1 .mqf.hexkid@ID-203069.user.uni-berlin.de...
          > > and gave some answer:[color=darkred]
          > >> pablo wrote:
          > >> > What I need ( imo ) is an array with objects that can be written[/color][/color][/color]
          later[color=blue][color=green]
          > > to a[color=darkred]
          > >> > dbfile to preserve.
          > >> >
          > >> > But I cannot find a solution for the extension of the array.
          > >> >
          > >> > the function statements are simply these:
          > >> >
          > >> > < inside function in included file >
          > >> > $o = someObject( $s ) ;
          > >> > $objarr[] = $o;
          > >> > </ inside function in included file >
          > >> >
          > >> > As this array is within a function it is local to this function.
          > >> > As I understand it now a good solution would be to pass the array to[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > >> > function.
          > >> > In the 'main' page I made a variable $objarr that is passed into the
          > >> > function like this:
          > >> > <'main' page>
          > >> > $objarr ;
          > >> > function( $objarr )
          > >> > </'main' page>
          > >> >
          > >> > The PHP manual states that in the absence of the array variable it[/color][/color][/color]
          gets[color=blue][color=green][color=darkred]
          > >> > created. {Does this mean that function( $objarr ) would suffice? }
          > >> > However,
          > >> > whatever I try, I end up with the creation of $objarr[ 0 ] only.
          > >> >
          > >> > Please advise
          > >>
          > >>
          > >> Pass the function parameter by reference:
          > >>
          > >> pedro$ cat xx.php
          > >> <?php
          > >> function more_array(&$ar r) {
          > >> // reference _____^_____
          > >> $arr[] = 'one';
          > >> $arr[] = 'two';
          > >> }
          > >>
          > >> more_array($x);
          > >> print_r($x);
          > >> ?>
          > >>
          > >> pedro$ php xx.php
          > >> Array
          > >> (
          > >> [0] => one
          > >> [1] => two
          > >> )[/color][/color]
          >[color=green]
          > > First of all thanks for the answer, but I find it difficult to[/color][/color]
          understand.[color=blue][color=green]
          > > I tried to follow your example but it doesn't solve my problem.
          > > Your array $x seems to exist already outside the function.[/color]
          >
          > It is created right before the more_array() function is called.
          >
          > Right before the script gets to the line
          > more_array($x);
          >
          > $x is undefined.
          > For PHP to be able to call the function, because the function expects a
          > reference, the parameter *must* exist, so it is created right there.
          >[color=green]
          > > As I see it I can go about in this way:
          > > $x[] = "three";[/color]
          >
          > If $x was undefined before last line, it is now an array with an element
          > with index 0 which holds the string "three".
          >[color=green]
          > > more_array( $x ) ;[/color]
          >
          > Calling print_r($x) now would output
          > [0] => three
          > [1] => one
          > [2] => two
          >[color=green]
          > > And I suspect the function is not necessary.[/color]
          >
          > ??????
          >[color=green]
          > > But what I want to add to the array is contained and created inside the
          > > function.[/color]
          >
          > Right! That is why you should use references :)
          > http://www.php.net/manual/en/language.references.php
          >
          > --
          > USENET would be a better place if everybody read: | to email me: use |
          > http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
          > http://www.netmeister.org/news/learn2quote2.html | header, textonly |
          > http://www.expita.com/nomime.html | no attachments. |[/color]

          I think I can smile as well now ;).
          Reworked your code to:
          function more_array( &$arr ) {
          static $n = 0 ;
          $cnt = count( $arr) +1 ;
          $t = "$n. this text. Array contains $cnt element(s). " ;
          $arr[] = $t ;
          $n++ ;
          }
          And that did wonders to understand what is going on.

          So, along this route I reworked my own code....
          BUT in my code I still see only item 0 gets created and the count reveals
          only this item?






          Comment

          • Pedro Graca

            #6
            Re: Function Parameter passing? Session? Static? Global?

            pablo wrote:[color=blue]
            > I think I can smile as well now ;).[/color]

            Good!
            [color=blue]
            > Reworked your code to:
            > function more_array( &$arr ) {
            > static $n = 0 ;
            > $cnt = count( $arr) +1 ;
            > $t = "$n. this text. Array contains $cnt element(s). " ;
            > $arr[] = $t ;
            > $n++ ;
            > }
            > And that did wonders to understand what is going on.[/color]

            You're on the right track :)
            [color=blue]
            > So, along this route I reworked my own code....
            > BUT in my code I still see only item 0 gets created and the count reveals
            > only this item?[/color]

            Make a copy of your script, delete all unnecessary stuff from it
            until you get a /minimal/ script that exhibits the error. Correct the
            error in the minimal script then apply the same changes to your full
            script.
            You may even find why the error crept in while you strip the code :)

            Alternatively, post your code here (after the stripping, please) and
            someone will surely tell you where it is going wrong.


            --
            USENET would be a better place if everybody read: | to email me: use |
            http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
            http://www.netmeister.org/news/learn2quote2.html | header, textonly |
            http://www.expita.com/nomime.html | no attachments. |

            Comment

            • pablo

              #7
              Re: Function Parameter passing? Session? Static? Global?

              "Pedro Graca" <hexkid@hotpop. com> wrote in message
              news:slrnccu8f6 .mqf.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
              >
              > You're on the right track :)
              >[color=green]
              > > So, along this route I reworked my own code....
              > > BUT in my code I still see only item 0 gets created and the count[/color][/color]
              reveals[color=blue][color=green]
              > > only this item?[/color]
              >
              > Make a copy of your script, delete all unnecessary stuff from it
              > until you get a /minimal/ script that exhibits the error. Correct the
              > error in the minimal script then apply the same changes to your full
              > script.
              > You may even find why the error crept in while you strip the code :)
              >
              > Alternatively, post your code here (after the stripping, please) and
              > someone will surely tell you where it is going wrong.[/color]

              Thanks Pedro!

              On the right track. But the track is longer than this problem. Well I don't
              mind as long as I keep learning. The goal of traveling is doing just that.

              My original question contained more (problems). And I think I see the origin
              of the error already.
              If I need help again I will post it here.

              Thanks again.

              pablo


              Comment

              Working...