Get name of passed variable in a function

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

    Get name of passed variable in a function

    I've cobbled a sort of global "debug" routine by using this function:

    function debug($str, $strname) {
    global $debugYN;
    if ($debugYN) {
    echo "<br>[Debug (<span
    style=\"color:r ed\">$$strname </span>) : <strong>$str</strong>]<br>";
    }
    }

    This function gets included (usually in a header) and then called by
    setting $debugYN = 1 wherever I want to start viewing variable info -
    calling the function with:

    debug($foo, "foo");

    I'm having to pass two values - the value of the variable AND it's
    name. Is there a way of extracting the NAME of the variable ("foo" in
    this case) without having to pass it?

    Adam.
  • Andy Hassall

    #2
    Re: Get name of passed variable in a function

    On Tue, 04 Oct 2005 09:14:21 +1300, Adam <anon@nowhere.c om> wrote:
    [color=blue]
    >function debug($str, $strname) {
    >
    >debug($foo, "foo");
    >
    >I'm having to pass two values - the value of the variable AND it's
    >name. Is there a way of extracting the NAME of the variable ("foo" in
    >this case) without having to pass it?[/color]

    In short, no.
    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Ewoud Dronkert

      #3
      Re: Get name of passed variable in a function

      Andy Hassall wrote:[color=blue]
      > In short, no.[/color]

      Yes, http://php.net/debug-backtrace

      --
      E. Dronkert

      Comment

      • Andy Hassall

        #4
        Re: Get name of passed variable in a function

        On Mon, 03 Oct 2005 22:29:48 +0200, Ewoud Dronkert
        <firstname@last name.net.invali d> wrote:
        [color=blue]
        >Andy Hassall wrote:[color=green]
        >> In short, no.[/color]
        >
        >Yes, http://php.net/debug-backtrace[/color]

        No,

        <pre>
        <?php
        function debug($var) {
        print_r(debug_b acktrace());
        }

        $foo = 10;
        debug($foo);

        ?>
        </pre>

        Output:

        Array
        (
        [0] => Array
        (
        [file] => /home/andyh/public_html/test.php
        [line] => 8
        [function] => debug
        [args] => Array
        (
        [0] => 10
        )

        )

        )

        If you can find "foo" in the above then it would do what the OP requested:
        [color=blue][color=green]
        >>I'm having to pass two values - the value of the variable AND it's
        >>name. Is there a way of extracting the NAME of the variable ("foo" in
        >>this case) without having to pass it?[/color][/color]
        --
        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

        Comment

        • Chung Leong

          #5
          Re: Get name of passed variable in a function

          See the following thread:



          Comment

          • Ewoud Dronkert

            #6
            Re: Get name of passed variable in a function

            Andy Hassall wrote:[color=blue]
            > On Mon, 03 Oct 2005 22:29:48 +0200, I wrote:[color=green]
            >> Yes, http://php.net/debug-backtrace[/color]
            >
            > No,[/color]

            Sorry, I remembered the discussion Chung linked and skimmed over the
            debug-backtrace manual page, saw the 'args' key and thought that was it.
            It wasn't.

            --
            E. Dronkert

            Comment

            • Adam

              #7
              Re: Get name of passed variable in a function

              On 3 Oct 2005 22:32:04 -0700, Chung Leong wrote:
              [color=blue]
              >See the following thread:
              >http://groups.google.com/group/comp....697d72cf31b316[/color]

              Chung - thank you for that! I had to do the array_shift tweak as I
              tend to use include files.

              For the benefit of readers here, I've pasted your code (with my tweak
              and thanks):

              function bobo_the_clown( $b) {
              $bt = debug_backtrace ();
              extract(array_s hift($bt));
              $lines = file($file);
              $code = implode('', array_slice($li nes, $line - 1));
              preg_match('/\bbobo_the_clow n\s*\(\s*(\S*?) \s*\)/i', $code,
              $matches);
              return @$matches[1] . " : " . $b;
              }

              $a = "Hello";
              echo bobo_the_clown( $a);

              Now, if only I could understand how it works .. LOL. Still so much to
              learn!

              Adam.

              Comment

              • R. Rajesh Jeba Anbiah

                #8
                Re: Get name of passed variable in a function

                Adam wrote:[color=blue]
                > On 3 Oct 2005 22:32:04 -0700, Chung Leong wrote:
                >[color=green]
                > >See the following thread:
                > >http://groups.google.com/group/comp....697d72cf31b316[/color]
                >
                > Chung - thank you for that! I had to do the array_shift tweak as I
                > tend to use include files.[/color]
                <snip>

                I wonder the use of this. Probably you're profiling? If so, better
                not reinventing the wheels, there are bunch of PHP core accessing
                packages like <http://pecl.php.net/packages.php?ca tpid=25&catname =PHP>

                --
                <?php echo 'Just another PHP saint'; ?>
                Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                Comment

                Working...