This is probably dumb but.. how can I find out the name of a variable? What I want is this:
Code:$var = 'bla';
$foo = array( 'yeah' );
print_name( $var ); // Print: var
print_name( $foo ); // Print: foo
Printable View
This is probably dumb but.. how can I find out the name of a variable? What I want is this:
Code:$var = 'bla';
$foo = array( 'yeah' );
print_name( $var ); // Print: var
print_name( $foo ); // Print: foo
echo ('foo'); -- Prints the name of your variable. You already know the name of the variable and the name of a variable does not change, therefore there is no need find it at runtime.
What is it you are trying to do? - maybe there is another way around your problem.
Sure there is... see this function:Quote:
therefore there is no need find it at runtime.
So I'm missing varname() here. I know that I could pass another parameter with the name of this variable but I'm trying to simplify things as well as possible. There must be a way to get the name of a referenced variable!Code:function print_field( $var )
{
print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
}
try this.. I just whipped it up.
I'd appreciate it if you left my author tag in there if you use it..PHP Code:<?
/*
* Author: David Miles - [email][email protected][/email]
* function varname([str varname])
* Returns the value of varname and inserts it into an HTML <input>
* Usage: varname("var") returns the value of $var
*/
function varname($str){
global ${$str};
return "<input name='$str' value='" . ${$str} . "'>";
}
?>
I threw in your <INPUT> code just for ease.. call it like:
and it will returnPHP Code:<? $thestr = "value";
echo "<xmp>" . varname("thestr") . "</xmp>";
?>
Hope it can helps you out..Code:<input name='thestr' value='value'>
edit: It sort of does the opposite of what you want.. but it should work for what you're doing..
kows - solution is best. You'll need to pass the variable name as a string.Quote:
Originally posted by Fox
Sure there is... see this function:
So I'm missing varname() here. I know that I could pass another parameter with the name of this variable but I'm trying to simplify things as well as possible. There must be a way to get the name of a referenced variable!Code:function print_field( $var )
{
print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
}
When you pass a variable to a function copy is made and the local variable comes into scope with the name you gave it in the function. So even if you could determine the name of the variable - it would be the name of the local function variable returned and not the global one.
yeah, exactly. I thought it'd be fairly easy, but found out that wasn't so when I stuck the variable into a temporary array as $tmp[$str] and then called a foreach($tmp as $key => $val).. it seemed like it'd work, and would've worked fine if it wasn't in a function. It kept returning the name of the variable that I defined in the function's parameters. Anyway, PHP doesn't recognize the fact that you passed a variable into your function.. it just stuck whatever the value of it was into the variable you referenced within the parameters and then goes on with executing the function. I don't think there's any way to grab the variable's name that's being referenced.
Hm that's right the referencing causes troubles...
Well guess I'll just leave it as it is now, that's passing the name as a string.
Thanks anyway!