function and variable names

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

    #1

    function and variable names

    suppose i have the following code

    function some_function($ data)
    {
    // do something with data
    }

    $test = 'some_value';

    some_function($ test);

    is there any way to access the var name '$test' from within the
    function, i. e. can $data find out that $test is it's "parent"?
    tia, micha

  • Virgil Green

    #2
    Re: function and variable names

    micha wrote:[color=blue]
    > suppose i have the following code
    >
    > function some_function($ data)
    > {
    > // do something with data
    > }
    >
    > $test = 'some_value';
    >
    > some_function($ test);
    >
    > is there any way to access the var name '$test' from within the
    > function, i. e. can $data find out that $test is it's "parent"?
    > tia, micha[/color]

    Don't think so... but why would you want to know that?

    --
    Virgil


    Comment

    • micha

      #3
      Re: function and variable names


      Virgil Green wrote:[color=blue]
      > micha wrote:[color=green]
      > > suppose i have the following code
      > >
      > > function some_function($ data)
      > > {
      > > // do something with data
      > > }
      > >
      > > $test = 'some_value';
      > >
      > > some_function($ test);
      > >
      > > is there any way to access the var name '$test' from within the
      > > function, i. e. can $data find out that $test is it's "parent"?
      > > tia, micha[/color]
      >
      > Don't think so... but why would you want to know that?
      >
      > --[/color]

      well, i'm trying to write a function that automatically validates
      arrays, i.e. $_POST, so i would need the original var name to report
      back errors.

      micha[color=blue]
      > Virgil[/color]

      Comment

      • Virgil Green

        #4
        Re: function and variable names

        micha wrote:[color=blue]
        > Virgil Green wrote:[color=green]
        >> micha wrote:[color=darkred]
        >>> suppose i have the following code
        >>>
        >>> function some_function($ data)
        >>> {
        >>> // do something with data
        >>> }
        >>>
        >>> $test = 'some_value';
        >>>
        >>> some_function($ test);
        >>>
        >>> is there any way to access the var name '$test' from within the
        >>> function, i. e. can $data find out that $test is it's "parent"?
        >>> tia, micha[/color]
        >>
        >> Don't think so... but why would you want to know that?
        >>
        >> --[/color]
        >
        > well, i'm trying to write a function that automatically validates
        > arrays, i.e. $_POST, so i would need the original var name to report
        > back errors.
        >
        > micha[color=green]
        >> Virgil[/color][/color]

        Then I'd suggest that your function have a second parameter that allows the
        caller to pass in a string of identifying data that you use in the "report"
        your function provides, or I'd have the function return the error messages
        and allow the caller to format their own "report" of the errors since the
        caller is aware of what variable was being validated.

        --
        Virgil


        Comment

        • Tony Marston

          #5
          Re: function and variable names


          "micha" <chotiwallah@we b.de> wrote in message
          news:1106814330 .870093.279030@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          >
          > Virgil Green wrote:[color=green]
          >> micha wrote:[color=darkred]
          >> > suppose i have the following code
          >> >
          >> > function some_function($ data)
          >> > {
          >> > // do something with data
          >> > }
          >> >
          >> > $test = 'some_value';
          >> >
          >> > some_function($ test);
          >> >
          >> > is there any way to access the var name '$test' from within the
          >> > function, i. e. can $data find out that $test is it's "parent"?
          >> > tia, micha[/color]
          >>
          >> Don't think so... but why would you want to know that?
          >>
          >> --[/color]
          >
          > well, i'm trying to write a function that automatically validates
          > arrays, i.e. $_POST, so i would need the original var name to report
          > back errors.[/color]

          As the $_POST array is an associative array of name=value pairs then why
          don't you do what I do and pass BOTH name and value to the validating
          function.

          --
          Tony Marston

          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




          Comment

          • Virgil Green

            #6
            Re: function and variable names

            Tony Marston wrote:[color=blue]
            > "micha" <chotiwallah@we b.de> wrote in message
            > news:1106814330 .870093.279030@ f14g2000cwb.goo glegroups.com.. .[color=green]
            >>
            >> Virgil Green wrote:[color=darkred]
            >>> micha wrote:
            >>>> suppose i have the following code
            >>>>
            >>>> function some_function($ data)
            >>>> {
            >>>> // do something with data
            >>>> }
            >>>>
            >>>> $test = 'some_value';
            >>>>
            >>>> some_function($ test);
            >>>>
            >>>> is there any way to access the var name '$test' from within the
            >>>> function, i. e. can $data find out that $test is it's "parent"?
            >>>> tia, micha
            >>>
            >>> Don't think so... but why would you want to know that?
            >>>
            >>> --[/color]
            >>
            >> well, i'm trying to write a function that automatically validates
            >> arrays, i.e. $_POST, so i would need the original var name to report
            >> back errors.[/color]
            >
            > As the $_POST array is an associative array of name=value pairs then
            > why don't you do what I do and pass BOTH name and value to the
            > validating function.[/color]

            I believe he wants the name of the array... "$_POST" in this case.

            --
            Virgil


            Comment

            • anima

              #7
              Re: function and variable names

              If you use `global $test;` as a line inside the function it will allow
              that function access to the var outside the function. if you did this
              after you assigned it's passed-on variable to a different name, you
              could compare the two non ?

              Comment

              • micha

                #8
                Re: function and variable names

                yes, how stupid of me.
                of course i can just use the names of the associative array.
                sorry to have bothered everybody

                micha

                Comment

                • Virgil Green

                  #9
                  Re: function and variable names

                  micha wrote:[color=blue]
                  > yes, how stupid of me.
                  > of course i can just use the names of the associative array.
                  > sorry to have bothered everybody
                  >
                  > micha[/color]

                  I fail to see how that will accomplish your original goal. Or was the
                  original problem mis-stated (duplicated below)?
                  [color=blue]
                  > micha wrote:[color=green]
                  >> suppose i have the following code
                  >>
                  >> function some_function($ data)
                  >> {
                  >> // do something with data
                  >> }
                  >>
                  >> $test = 'some_value';
                  >>
                  >> some_function($ test);
                  >>
                  >> is there any way to access the var name '$test' from within the
                  >> function, i. e. can $data find out that $test is it's "parent"?[/color][/color]


                  --
                  Virgil


                  Comment

                  Working...