Jump to content

Convert Variable name into string


slpwkr

Recommended Posts

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
Share on other sites

I'm bit confused with you saying without the $ because aint just just

echo "variablename";

above will read as: variablename

or did you mean actualy echo $variablename e.g.

echo '$variablename';

above will read as: $variablename


Nott 100% on what you mean?
Link to comment
Share on other sites

its like this:

[code]
$variablename= 'whatever';

echo $variablename;

# output: Whatever

echo '$variablename';

# output: $variablename

echo 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
Share on other sites

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
Share on other sites

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
Share on other sites

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]<?php
function 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.