Echo the actual Variable name

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

    Echo the actual Variable name

    So i sit here thinking, and wondering...

    say for example i have a variable called $definition_lon don

    Say for exaple that has not set value...

    Is it possible to echoe thew ACTUAL variable name into a document, rather
    than the variables information?

    Thanks for yout time, look forward to all replies :)


  • Kimmo Laine

    #2
    Re: Echo the actual Variable name

    "Dawsons" <stuart@3branch .co.uk> wrote in message
    news:AahKf.3246 2$Fy4.29398@new sfe4-win.ntli.net...[color=blue]
    > So i sit here thinking, and wondering...
    >
    > say for example i have a variable called $definition_lon don
    >
    > Say for exaple that has not set value...
    >
    > Is it possible to echoe thew ACTUAL variable name into a document, rather
    > than the variables information?
    >
    > Thanks for yout time, look forward to all replies :)
    >[/color]


    I'm no sure what you are exactly looking for, but you could do something
    like this:

    echo 'Here is a list of all variables and their values defined in this
    script:<br />';
    print_r(get_def ined_vars());

    --
    "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • samudasu

      #3
      Re: Echo the actual Variable name

      echo '$definition_lo ndon is in a string';

      Comment

      • Ewoud Dronkert

        #4
        Re: Echo the actual Variable name

        Dawsons wrote:[color=blue]
        > Is it possible to echoe thew ACTUAL variable name into a document[/color]

        function getvarname(&$va r)
        {
        $ret = '';
        $tmp = $var;
        $var = md5(uniqid(rand (), TRUE));

        $key = array_keys($GLO BALS);
        foreach ( $key as $k )
        if ( $GLOBALS[$k] === $var )
        {
        $ret = $k;
        break;
        }

        $var = $tmp;
        return $ret;
        }

        $another = 'test';
        $testvar = 'test';
        echo getvarname($tes tvar); //echoes 'testvar'

        --
        E. Dronkert

        Comment

        • Dawsons

          #5
          Re: Echo the actual Variable name

          Thank you very much, this is excellent!

          "Ewoud Dronkert" <firstname@last name.net.invali d> wrote in message
          news:dd6kv19jkb etderhetu39ioqk vq6nuta4n@4ax.c om...[color=blue]
          > Dawsons wrote:[color=green]
          >> Is it possible to echoe thew ACTUAL variable name into a document[/color]
          >
          > function getvarname(&$va r)
          > {
          > $ret = '';
          > $tmp = $var;
          > $var = md5(uniqid(rand (), TRUE));
          >
          > $key = array_keys($GLO BALS);
          > foreach ( $key as $k )
          > if ( $GLOBALS[$k] === $var )
          > {
          > $ret = $k;
          > break;
          > }
          >
          > $var = $tmp;
          > return $ret;
          > }
          >
          > $another = 'test';
          > $testvar = 'test';
          > echo getvarname($tes tvar); //echoes 'testvar'
          >
          > --
          > E. Dronkert[/color]


          Comment

          • Oli Filth

            #6
            Re: Echo the actual Variable name

            Dawsons said the following on 20/02/2006 10:51:[color=blue]
            > So i sit here thinking, and wondering...
            >
            > say for example i have a variable called $definition_lon don
            >
            > Say for exaple that has not set value...
            >
            > Is it possible to echoe thew ACTUAL variable name into a document, rather
            > than the variables information?
            >[/color]

            Maybe I'm missing something, but why can't you just do echo
            '$definition_lo ndon'; ??

            --
            Oli

            Comment

            • Ewoud Dronkert

              #7
              Re: Echo the actual Variable name

              Dawsons wrote:[color=blue]
              > Thank you very much, this is excellent![/color]

              Only works for globals though.

              --
              E. Dronkert

              Comment

              • Iván Sánchez Ortega

                #8
                Re: Echo the actual Variable name

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                Dawsons wrote:
                [color=blue]
                > say for example i have a variable called $definition_lon don
                >
                > Say for exaple that has not set value...[/color]

                If it has no value, that varable name does not exist. Bye-bye.
                [color=blue]
                > Is it possible to echoe thew ACTUAL variable name into a document, rather
                > than the variables information?[/color]

                No, because a variable can have more than one, due to the way PHP handles
                references. Suppose the following code:

                <?php

                $a = 42;

                $b =& $a;
                $c =& $a;
                unset ($a);

                ?>

                At this point, what is the name of the variable that holds "42"?? $b, $c,
                both, or nobody??

                For this issue, do not think that a variable is a variable. Think that a
                variable is an entry in an ordered map (with internal pointers),
                implemented as a hash table that can map two (or more) keys to the same
                value.

                Let me (try to) explain this concept in pseudo-C:

                function foo()
                {
                int* a;
                int* b;
                inc* c;
                a = b = c = malloc(sizeof(i nt));
                *a = 42;
                a = NULL;
                }

                This would have a similar behaviour than the PHP code I posted up there. In
                PHP, variable names "point" to the value. A reference is a pointer to that
                value. Storing a reference is making two entries on the variable hash
                tables point to the same value. When no references to a value are found,
                that memory is freed.

                Variable management in PHP is quite complicated internally, with all that
                duplicate-on-write stuff when making copies of complex variables. The kind
                of stuff that operating systems do when you fork() a process.



                I agree with Ewoud: you can transverse the $GLOBALS superglobal array to
                search for your value, but if you have any references over there, you may
                find lots of problems. Lots.

                - --
                - ----------------------------------
                Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


                Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
                5.1.2-1 generating this signature.
                Uptime: 00:59:10 up 48 min, 1 user, load average: 0.78, 0.74, 0.74

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.2 (GNU/Linux)

                iD8DBQFD+lyN3jc Q2mg3Pc8RAvKSAJ 0WnvCHl3pnSbriF UUpt2GaubcdYACf eTLZ
                LN4aiZoWerYGDo+ 5lrF2Bk0=
                =OrfI
                -----END PGP SIGNATURE-----

                Comment

                Working...