Welcome to WebmasterWorld Guest from 184.108.40.206 , register , free tools , login , search , subscribe , help , library , announcements , recent posts , open posts Subscribe to WebmasterWorld
Function Question getting variable name and value from one argument rover msg:1301218 4:52 pm on Dec 29, 2005 (gmt 0) I have a function that requires both the value of the field and its name. Now I'm calling it as follows, where $field is a variable defined in the script that the function is called:
Am I stuck with needing to use both arguments, or is there any way that I can just use one argument (field), and that the function can then be structured to get the actual value of $field from the script where that value is defined?
Or is there just no way that a function can use variables that are defined in the script (other than as a function argument)?
jatar_k msg:1301219 5:11 pm on Dec 29, 2005 (gmt 0)
this thread from the PHP Library [ webmasterworld.com] might shed some light Understanding Custom Functions and Variable Scope in PHP [ webmasterworld.com] rover msg:1301220 6:23 pm on Dec 29, 2005 (gmt 0)
Thanks those links were very helpful. I have an idea of how to do this now, but still don't know if there is a way to extract out a variable name.
For example if I have
So the variable color has the value of red. Is there anyway to extract the actual variable name from $color? (I just want the 'color' part) So that I could finally have something like:
$y = "color";
rover msg:1301221 7:06 pm on Dec 29, 2005 (gmt 0)
Sorry about that, I was overthinking things. I can do it with:
$y = '$color';
$y = str_replace("$", "", $y);
Mr_Fern msg:1301222 9:35 pm on Dec 29, 2005 (gmt 0)
Possible Plan of Action
$field_value = $_GLOBALS[$field_name]; // get value of field variable
// sample function action
echo '$'. $field_name .' contains the value: '. $field_value;
// end sample
Now say you have $j, that has the value 5.
"$j contains the value: 5"
I'm confused what the purpose is of the function, but the code above would help you get the variable value with just the variable name alone.