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 " :</div></td><td valign='top'><div align='left'> ";
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 " :</div></td><td valign='top'><div align='left'> ";
echo $f_value;
echo "</div></td></tr>";
}
echo "</table>";
}
Thank you.
>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###
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/
> 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
Oh yes sorry, of course. On the other hand, the function shouldn't fail
unless the variable was undefined :)
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 " :</div></td><td valign='top'><div align='left'> ";
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 " :</div></td><td valign='top'><div
align='left'> ";
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
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 " </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 " :</div></td><td valign='top'><div
align='left'> ";
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 " :</div></td><td valign='top'><div align='left'> ";
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>";
}
//---------------------------------------------------------------------------
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.