Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you get variable name of argument? 1

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
Is there a way to the actual variable name that was used to pass to the function (if any)?

Here's an example of what I mean:
Code:
$variable_name = "Some value.";

some_function( $variable_name );

function some_function ( $arg )
{
   // Here is where I would probably call a function to
   // get the actual name of the variable used to set
   // $arg when it was called.
   $name = get_var_name( $arg );

   // Print actual name:
   print( $name );
}

[tt]some_function()[/tt] should have printed "variable_name".

---------------------------------------
 
No, I'm serious. I've been programming for more than two decades in any of two or three dozen languages, and I've never needed that information.

Of what could that information be?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Are you trying to behave differently depending on what variable was passed - perhaps to modify the value of the variable? If so, that is better achieved by a pass-by-reference argument, which is really easy in PHP - just precede the formal argument name in the function definiton with '&', for example:

Code:
function func(&arg)
{
  arg++;
}
.
.
.
v := 0;
echo(v); // v shows 0
func(v);
echo(v); // v shows 1
.
.
.
 
What I want is for debugging purposes. I want to be able to dynamically tell the programmer what variable has what problems.

I tell the problems but I don't know how to get the name of the variables that they are using without adding extra work for them.

---------------------------------------
 
The simple way is to put the debug code in-line, such as:

echo("var1=$var1 var2=$var2");

The double quotes cause the variable names to be 'expanded' into their values.

I guess you could send the string to a function but I'm not sure when PHP does the variable expansion - at parse or execution time.
 
Zend has a quite useful IDE (Studio) which contains the Zend debugger. It is actually very helpful in debugging code.
You can download it as a trial and use the capabilities for a limited time. For a professional setup the expense is worth it if you do a lot of debugging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top