getting the variable name in php

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

    getting the variable name in php

    I have created a function, it gives more readability compared to the
    print_r function. As of print_r, it works both for array or single
    variable. I just want to add in it, the opton to view the variable
    name for the case of non array variables. Also I want to show the
    array name.

    Is there any way that the variable name that is passed to the function
    can be displayed.

    function formattedoutput ($object) {

    echo "<table style='border:1 px #cccccc solid'>";

    if(is_array($ob ject)) {
    ksort($object);
    reset($object);
    foreach($object as $f_name=>$f_val ue) {
    echo "<tr><td valign='top'><d iv align='right'>" ;
    if(!$f_value) { echo "<font color='red'>"; }
    echo $f_name;
    if(!$f_value) { echo "</font>"; }
    echo "&nbsp;:</div></td><td valign='top'><d iv align='left'>&n bsp;";
    if(is_array($f_ value)) {
    formattedoutput ($f_value);
    } else {
    echo $f_value;
    }
    echo "</div></td></tr>";
    }
    } else {
    $f_name = ""; // variable name
    $f_value = $object;
    echo "<tr><td valign='top'><d iv align='right'>" ;
    if(!$f_value) { echo "<font color='red'>"; }
    echo $f_name;
    if(!$f_value) { echo "</font>"; }
    echo "&nbsp;:</div></td><td valign='top'><d iv align='left'>&n bsp;";
    echo $f_value;
    echo "</div></td></tr>";
    }

    echo "</table>";

    }

    Thank you.
  • James

    #2
    Re: getting the variable name in php

    On 2 Jun 2005 04:40:03 -0700, yehaimanish@gma il.com (manish) wrote:
    [color=blue]
    >Is there any way that the variable name that is passed to the function
    >can be displayed.[/color]

    Someone has wrote some info on how to do this here, as a user
    contributed note:


    I will paste here if you can't find it, credit goes to lucas karisny.

    ###start paste###
    Here's a function to get the name of a given variable. Explanation
    and examples below.

    <?php
    function vname(&$var, $scope=false, $prefix='unique ',
    $suffix='value' )
    {
    if($scope) $vals = $scope;
    else $vals = $GLOBALS;
    $old = $var;
    $var = $new = $prefix.rand(). $suffix;
    $vname = FALSE;
    foreach($vals as $key => $val) {
    if($val === $new) $vname = $key;
    }
    $var = $old;
    return $vname;
    }
    ?>

    Explanation:

    The problem with figuring out what value is what key in that variables
    scope is that several variables might have the same value. To remedy
    this, the variable is passed by reference and its value is then
    modified to a random value to make sure there will be a unique match.
    Then we loop through the scope the variable is contained in and when
    there is a match of our modified value, we can grab the correct key.

    Examples:

    1. Use of a variable contained in the global scope (default):
    <?php
    $my_global_vari able = "My global string.";
    echo vname($my_globa l_variable); // Outputs: my_global_varia ble
    ?>

    2. Use of a local variable:
    <?php
    function my_local_func()
    {
    $my_local_varia ble = "My local string.";
    return vname($my_local _variable, get_defined_var s());
    }
    echo my_local_func() ; // Outputs: my_local_variab le
    ?>

    3. Use of an object property:
    <?php
    class myclass
    {
    public function __constructor()
    {
    $this->my_object_prop erty = "My object property string.";
    }
    }
    $obj = new myclass;
    echo vname($obj->my_object_prop erty, $obj); // Outputs:
    my_object_prope rty
    ?>
    ###endpaste###


    Comment

    • Ewoud Dronkert

      #3
      Re: getting the variable name in php

      On Thu, 02 Jun 2005 13:09:31 +0100, James wrote:[color=blue]
      > Someone has wrote some info on how to do this here, as a user
      > contributed note: http://php.net/variables
      > [...] credit goes to lucas karisny.[/color]

      Yeah. If you have no need for local scope you can trim the function a
      little. Also with simpler unique value generation:

      function f( &$x )
      {
      $old = $x;
      $x = md5( uniqid( mt_rand(), TRUE ) );
      foreach ( $GLOBALS as $k => $v )
      if ( $v === $x )
      {
      $x = $old;
      return $k;
      }
      return FALSE;
      }

      $a = 'test';
      $b = 'test';
      echo f( $a ); // 'a'
      echo f( $b ); // 'b'


      --
      Firefox Web Browser - Rediscover the web - http://getffox.com/
      Thunderbird E-mail and Newsgroups - http://gettbird.com/

      Comment

      • Janwillem Borleffs

        #4
        Re: getting the variable name in php

        Ewoud Dronkert wrote:
        [color=blue]
        > On Thu, 02 Jun 2005 13:09:31 +0100, James wrote:
        >[color=green]
        >>Someone has wrote some info on how to do this here, as a user
        >>contributed note: http://php.net/variables
        >>[...] credit goes to lucas karisny.[/color]
        >
        >
        > Yeah. If you have no need for local scope you can trim the function a
        > little. Also with simpler unique value generation:
        >
        > function f( &$x )
        > {
        > $old = $x;
        > $x = md5( uniqid( mt_rand(), TRUE ) );
        > foreach ( $GLOBALS as $k => $v )
        > if ( $v === $x )
        > {
        > $x = $old;
        > return $k;
        > }
        > return FALSE;
        > }
        >
        > $a = 'test';
        > $b = 'test';
        > echo f( $a ); // 'a'
        > echo f( $b ); // 'b'
        >
        >[/color]

        $x should be assigned its old value before returning FALSE; otherwise it
        will be modified when the function failed...


        JW


        Comment

        • Ewoud Dronkert

          #5
          Re: getting the variable name in php

          On Thu, 02 Jun 2005 16:25:55 +0200, Janwillem Borleffs wrote:[color=blue]
          > $x should be assigned its old value before returning FALSE; otherwise
          > it will be modified when the function failed...[/color]

          Oh yes sorry, of course. On the other hand, the function shouldn't fail
          unless the variable was undefined :)


          --
          Firefox Web Browser - Rediscover the web - http://getffox.com/
          Thunderbird E-mail and Newsgroups - http://gettbird.com/

          Comment

          • Chung Leong

            #6
            Re: getting the variable name in php

            Yeah. Use debug_backtrace () to see what file and at which line the
            function was called, open the script, with file(), then preg_match()
            the line in question for the function name to see what the arguments
            were.

            Comment

            • manish

              #7
              Re: getting the variable name in php

              Thanks for referring to the function vname(&$var, $scope=false,
              $prefix='unique ', $suffix='value' ) and to f( &$x ). They worked fine,
              but I want something else. It gives the name of the variable as the
              name of the parameter in the function itself. May be we can pass the
              variable name itself as the parameter to the function.

              I am submitting what I have modified in the function. I think there is
              something wrong in the function formattedoutput () itself.


              //------------------------------------------------------------

              function vname(&$var, $scope=false, $prefix='unique ', $suffix='value' )
              {
              if($scope) $vals = $scope;
              else $vals = $GLOBALS;
              $old = $var;
              $var = $new = $prefix.rand(). $suffix;
              $vname = FALSE;

              foreach($vals as $key => $val) {
              if($val === $new) $vname = $key;
              }
              $var = $old;
              return $vname;
              }

              function f( &$x )
              {
              $old = $x;
              $x = md5( uniqid( mt_rand(), TRUE ) );
              foreach ( $GLOBALS as $k => $v )
              if ( $v === $x )
              {
              $x = $old;
              return $k;
              } return FALSE;
              }

              function formattedoutput ($object, $varname="") {

              echo "<table style='border:1 px #cccccc solid'>";

              if(is_array($ob ject)) {
              ksort($object);
              reset($object);

              $f_name = ""; //name of the array variable (NOT YET DONE)

              echo "<tr><td valign='top'><d iv align='right'>" ;
              if(!count($obje ct)) { echo "<font color='red'>"; }
              echo "<b>".$f_name." </b>";
              if(!count($obje ct)) { echo "</font>"; }
              echo "&nbsp;:</div></td><td valign='top'><d iv align='left'>&n bsp;";
              echo "</div></td></tr>";

              if(count($objec t)) {
              foreach($object as $f_name=>$f_val ue) {
              echo "<tr><td valign='top'><d iv align='right'>" ;
              if(!$f_value) { echo "<font color='red'>"; }
              echo $f_name;
              if(!$f_value) { echo "</font>"; }
              echo "&nbsp;:</div></td><td valign='top'><d iv
              align='left'>&n bsp;";
              if(is_array($f_ value)) {
              formattedoutput ($f_value);
              } else {
              echo $f_value;
              }
              echo "</div></td></tr>";
              }
              }

              } else {
              $f_name = $varname; // variable name, comes from the function
              parameter (NOT YET DONE)
              $f_value = $object;

              //$f_name = f($object); //DOESN'T GIVE ANY OUTPUT
              //$f_name = vname($object, get_defined_var s()); //GIVES THE VARIABLE
              NAME AS OBJECT

              echo "<tr><td valign='top'><d iv align='right'>" ;
              if(!$f_value) { echo "<font color='red'>"; }
              echo $f_name;
              if(!$f_value) { echo "</font>"; }
              echo "&nbsp;:</div></td><td valign='top'><d iv align='left'>&n bsp;";
              echo $f_value;
              echo "</div></td></tr>";
              }
              echo "</table>";

              }

              Comment

              • manish

                #8
                Re: getting the variable name in php

                Thanks for referring the functions vname(&$var, $scope=false,
                $prefix='unique ', $suffix='value' ), f( &$x ), debug_backtrace ().

                I guess there is some error in the formattedoutput ($object,
                $varname="") function itself. The debug_backtrace () will solve the
                problem, but then the solution will be somewhat complicated.

                //---------------------------------------------------------------------------
                function vname(&$var, $scope=false, $prefix='unique ', $suffix='value' )
                {
                if($scope) $vals = $scope;
                else $vals = $GLOBALS;
                $old = $var;
                $var = $new = $prefix.rand(). $suffix;
                $vname = FALSE;

                foreach($vals as $key => $val) {
                if($val === $new) $vname = $key;
                }
                $var = $old;
                return $vname;
                }

                function f( &$x )
                {
                $old = $x;
                $x = md5( uniqid( mt_rand(), TRUE ) );
                foreach ( $GLOBALS as $k => $v )
                if ( $v === $x )
                {
                $x = $old;
                return $k;
                } return FALSE;
                }

                function formattedoutput ($object, $varname="") {

                echo "<table style='border:1 px #cccccc solid'>";

                if(is_array($ob ject)) {
                ksort($object);
                reset($object);

                $f_name = $varname; //name of the array variable (NOT YET DONE)

                echo "<tr><td valign='top' colspan='2'><di v align='center'> ";
                if(!count($obje ct)) { echo "<font color='red'>"; }
                echo "<b>".$f_name." </b>";
                if(!count($obje ct)) { echo "</font>"; }
                echo "&nbsp;</div></td></tr>";

                if(count($objec t)) {
                foreach($object as $f_name=>$f_val ue) {
                echo "<tr><td valign='top'><d iv align='right'>" ;
                if(!$f_value) { echo "<font color='red'>"; }
                echo $f_name;
                if(!$f_value) { echo "</font>"; }
                echo "&nbsp;:</div></td><td valign='top'><d iv
                align='left'>&n bsp;";
                if(is_array($f_ value)) {
                formattedoutput ($f_value);
                } else {
                echo $f_value;
                }
                echo "</div></td></tr>";
                }
                }

                } else {
                $f_name = $varname; // variable name, comes from the function
                parameter (NOT YET DONE)
                $f_value = $object;

                //$f_name = f($object); //DOESN'T GIVE ANY OUTPUT
                //$f_name = vname($object, get_defined_var s()); //GIVES THE VARIABLE
                NAME AS OBJECT

                echo "<tr><td valign='top'><d iv align='right'>" ;
                if(!$f_value) { echo "<font color='red'>"; }
                echo $f_name;
                if(!$f_value) { echo "</font>"; }
                echo "&nbsp;:</div></td><td valign='top'><d iv align='left'>&n bsp;";
                echo $f_value;
                echo "</div></td></tr>";
                }

                echo "</table>";

                echo "<pre>";
                print_r(debug_b acktrace()); // [file] =>
                D:\public_html\ games\admin\use rs.php
                echo "</pre>";

                }
                //---------------------------------------------------------------------------

                Comment

                • Ewoud Dronkert

                  #9
                  Re: getting the variable name in php

                  On 2 Jun 2005 21:11:36 -0700, manish wrote:[color=blue]
                  > It gives the name of the variable as the
                  > name of the parameter in the function itself.
                  >
                  > function formattedoutput ($object, $varname="") {
                  > [...]
                  > //$f_name = f($object); //DOESN'T GIVE ANY OUTPUT
                  > //$f_name = vname($object, get_defined_var s()); //GIVES THE VARIABLE[/color]

                  Yeah sure. You called them with $object, so they return "object" (or
                  nothing because f() does not do local scope). What you need to do is
                  _combine_ either function with your own, not just call them. You need to
                  implement their functionality in your formattedoutput () function.


                  --
                  Firefox Web Browser - Rediscover the web - http://getffox.com/
                  Thunderbird E-mail and Newsgroups - http://gettbird.com/

                  Comment

                  Working...