|
-
Mar 21st, 2004, 05:51 PM
#1
Thread Starter
PowerPoster
Get_Variable_Name
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
-
Mar 21st, 2004, 06:23 PM
#2
Re: Get_Variable_Name
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.
-
Mar 21st, 2004, 06:44 PM
#3
Thread Starter
PowerPoster
therefore there is no need find it at runtime.
Sure there is... see this function:
Code:
function print_field( $var )
{
print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
}
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!
-
Mar 21st, 2004, 08:05 PM
#4
try this.. I just whipped it up.
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'd appreciate it if you left my author tag in there if you use it..
I threw in your <INPUT> code just for ease.. call it like:
PHP Code:
<? $thestr = "value";
echo "<xmp>" . varname("thestr") . "</xmp>";
?>
and it will return
Code:
<input name='thestr' value='value'>
Hope it can helps you out..
edit: It sort of does the opposite of what you want.. but it should work for what you're doing..
Last edited by kows; Mar 21st, 2004 at 08:12 PM.
-
Mar 22nd, 2004, 02:50 AM
#5
Originally posted by Fox
Sure there is... see this function:
Code:
function print_field( $var )
{
print( '<input name="' . varname( $var ) . '" value="' . $var . '">' );
}
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!
kows - solution is best. You'll need to pass the variable name as a string.
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.
-
Mar 22nd, 2004, 05:27 AM
#6
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.
-
Mar 24th, 2004, 07:04 AM
#7
Thread Starter
PowerPoster
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|