slpwkr Posted March 1, 2006 Share Posted March 1, 2006 Hey guys is there a function to convert a variable name into string?Ex:[code]$variablename= 'whatever';[/code]I want to print the "variablename" only without the $ (dollor) sign and not using a substr() function. thank you! Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/ Share on other sites More sharing options...
shocker-z Posted March 1, 2006 Share Posted March 1, 2006 I'm bit confused with you saying without the $ because aint just just echo "variablename";above will read as: variablenameor did you mean actualy echo $variablename e.g.echo '$variablename';above will read as: $variablenameNott 100% on what you mean? Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-13241 Share on other sites More sharing options...
slpwkr Posted March 1, 2006 Author Share Posted March 1, 2006 its like this:[code]$variablename= 'whatever';echo $variablename;# output: Whateverecho '$variablename';# output: $variablenameecho substr('$variablename',1);# output: variablename <--- this is the one i need[/code]what I need is to do is... Use the $variablename and print variablename just like the 3rd example but not using substr. Is there any other way? Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-13246 Share on other sites More sharing options...
shocker-z Posted March 1, 2006 Share Posted March 1, 2006 str_replace('$','','$variablename');but thats identical to typing echo ('variablename');What you actualy need it for? like if u show some example of what your using it in a may be able to help.. Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-13254 Share on other sites More sharing options...
slpwkr Posted March 6, 2006 Author Share Posted March 6, 2006 Thanx, but I don't think what im trying to do is possible. Its something like this[code]function myfunc ($variable) {global $array;$array[$variable]=$variable;}$var1='my 1st string';$var2='my 2nd string';myfunct ($var1);myfunct ($var2);print_r($array);#I want an output something like this: ('var1'=>'this is my 1st string', 'var2'=>'this is my 2nd string')[/code] Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-14535 Share on other sites More sharing options...
shocker-z Posted March 6, 2006 Share Posted March 6, 2006 Wouldn't this work then?[code]function myfunc ($variable) {global $array;$variable2=str_replace('$','','$variable);$array["$variable2"]=$variable;}$var1='my 1st string';$var2='my 2nd string';myfunct ($var1);myfunct ($var2);print_r($array);#I want an output something like this: ('var1'=>'this is my 1st string', 'var2'=>'this is my 2nd string')[/code]Not sure if that's what your looking for.. Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-14587 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2006 Share Posted March 6, 2006 Try this code. It takes advantage of the superglobal array $GLOBALS. One thing you should be aware of is that if you have more than one variable with the same value, only the first one will be found.[code]<?phpfunction myfunc($var){ $defk = 'no_var'; foreach($GLOBALS as $k => $v) if (!is_array($k) && $v == $var) return($k); return($defk);} $test1 = "This is a test"; $yat = 'This is yet another test'; $yat2 = 'This is yet another test'; $new_array = array(); $new_array[myfunc($test1)] = $test1; $new_array[myfunc($yat)] = $yat; $new_array[myfunc($yat2)] = $yat2; $tmp = array(); foreach ($new_array as $k => $v) $tmp[] = "'" . $k . "' => '" . $v . "'"; echo '<pre>(' . implode(', ',$tmp) . ')</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/3815-convert-variable-name-into-string/#findComment-14649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.