By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

getting the variable name in php

P: n/a
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:1px #cccccc solid'>";

if(is_array($object)) {
ksort($object);
reset($object);
foreach($object as $f_name=>$f_value) {
echo "<tr><td valign='top'><div align='right'>";
if(!$f_value) { echo "<font color='red'>"; }
echo $f_name;
if(!$f_value) { echo "</font>"; }
echo "&nbsp;:</div></td><td valign='top'><div align='left'>&nbsp;";
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'><div align='right'>";
if(!$f_value) { echo "<font color='red'>"; }
echo $f_name;
if(!$f_value) { echo "</font>"; }
echo "&nbsp;:</div></td><td valign='top'><div align='left'>&nbsp;";
echo $f_value;
echo "</div></td></tr>";
}

echo "</table>";

}

Thank you.
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2 Jun 2005 04:40:03 -0700, ye*********@gmail.com (manish) wrote:
Is there any way that the variable name that is passed to the function
can be displayed.


Someone has wrote some info on how to do this here, as a user
contributed note:
http://uk.php.net/language.variables

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_variable = "My global string.";
echo vname($my_global_variable); // Outputs: my_global_variable
?>

2. Use of a local variable:
<?php
function my_local_func()
{
$my_local_variable = "My local string.";
return vname($my_local_variable, get_defined_vars());
}
echo my_local_func(); // Outputs: my_local_variable
?>

3. Use of an object property:
<?php
class myclass
{
public function __constructor()
{
$this->my_object_property = "My object property string.";
}
}
$obj = new myclass;
echo vname($obj->my_object_property, $obj); // Outputs:
my_object_property
?>
###endpaste###
Jul 17 '05 #2

P: n/a
On Thu, 02 Jun 2005 13:09:31 +0100, James wrote:
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.


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/
Jul 17 '05 #3

P: n/a
Ewoud Dronkert wrote:
On Thu, 02 Jun 2005 13:09:31 +0100, James wrote:
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.

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'


$x should be assigned its old value before returning FALSE; otherwise it
will be modified when the function failed...
JW
Jul 17 '05 #4

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


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/
Jul 17 '05 #5

P: n/a
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.

Jul 17 '05 #6

P: n/a
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:1px #cccccc solid'>";

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

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

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

if(count($object)) {
foreach($object as $f_name=>$f_value) {
echo "<tr><td valign='top'><div align='right'>";
if(!$f_value) { echo "<font color='red'>"; }
echo $f_name;
if(!$f_value) { echo "</font>"; }
echo "&nbsp;:</div></td><td valign='top'><div
align='left'>&nbsp;";
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_vars()); //GIVES THE VARIABLE
NAME AS OBJECT

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

}
Jul 17 '05 #7

P: n/a
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:1px #cccccc solid'>";

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

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

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

if(count($object)) {
foreach($object as $f_name=>$f_value) {
echo "<tr><td valign='top'><div align='right'>";
if(!$f_value) { echo "<font color='red'>"; }
echo $f_name;
if(!$f_value) { echo "</font>"; }
echo "&nbsp;:</div></td><td valign='top'><div
align='left'>&nbsp;";
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_vars()); //GIVES THE VARIABLE
NAME AS OBJECT

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

echo "</table>";

echo "<pre>";
print_r(debug_backtrace()); // [file] =>
D:\public_html\games\admin\users.php
echo "</pre>";

}
//---------------------------------------------------------------------------
Jul 17 '05 #8

P: n/a
On 2 Jun 2005 21:11:36 -0700, manish wrote:
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_vars()); //GIVES THE VARIABLE


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/
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.