variable name needed to be converted to string

I'm looking for a way to covert a variable to a string. I thought I could use settype or cast or var_dump but all don't seem to convert the variable name only, so for example:-
Function convertMe ($parName) {
     //varName does not exist as a function!!!
     $varTemp = varName($parName);
     //Just echo out at the moment, but should be a string echoed. 
     echo $varTemp
     }

$myVariable = "hello this is a test";

//I would like to echo out myVariable
convertMe($myVariable);

Open in new window

I cannot see any examples, or google results as a solution, does anyone have any ideas?

I think I need to pass my variable as a reference,  but the the variable inside the function will be the name not the initial variable name, Im a little confused where even to start.

Thank you in advance
tonelm54Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ray PaseurConnect With a Mentor Commented:
I think this is a great question and it would be a very useful language feature for debugging.
http://www.laprbass.com/RAY_dump_r.php

<?php // RAY_dump_r.php
error_reporting(E_ALL);
echo "<pre>";


// MAN PAGE http://php.net/manual/en/language.variables.php#76245
// MAN PAGE http://php.net/manual/en/language.variables.php#49997


// FUNCTION TO DISPLAY THE VARIABLE NAME AND VALUE
function dump_r(&$var, $return=FALSE)
{
    // THE VARIABLE NAME THING WE ARE LOOKING FOR
    $vname = '?';

    // GET A COPY OF ALL OF THE PHP VARIABLES
    $vals = $GLOBALS;

    // SINCE WE PASS THE VAR BY REFERENCE, WE MUST SAVE A COPY OF THE OLD VALUE
    $old  = $var;

    // MUNG THE VALUE OF OUR VARIABLE TO A UNIQUE VALUE
    $var  = $new = 'UNIQ' . $_SERVER["UNIQUE_ID"] .'VALU';

    // FIND THE MUNGED VALUE IN THE COPY OF ALL OUR VARIABLES
    foreach($vals as $key => $val)
    {
        // LOOKING FOR ARRAY
        if (is_array($val))
        {
            foreach ($val as $akey => $aval)
            {
                if ($aval === $new) $vname = $key . '[' . $akey . ']';
            }
        }
        // LOOKING FOR OBJECT
        if (is_object($val))
        {
            foreach ($val as $okey => $oval)
            {
                if ($oval === $new) $vname = $key . '=>' . $okey;
            }
        }
        // LOOKING FOR SCALAR
        if ($val === $new) $vname = $key;
    }

    // RESTORE THE VAR TO ITS OLD VALUE
    $var = $old;

    // GET THE RESULTS
    ob_start();
    echo "<b>\${$vname}</b> = ";
    var_dump($var);
    $str = rtrim(ob_get_contents()) . PHP_EOL;
    ob_end_clean();

    // RETURN OR DISPLAY THE RESULTS?
    if ($return)
    {
        return $str;
    }
    else
    {
        echo $str;
    }
}


// DEMONSTRATE THE FUNCTION WITH SOME TEST CASES
unset($x);
dump_r($x);

// A SCALAR VARIABLE
$scaly = "ABC";
dump_r($scaly);

// AN ARRAY VARIABLE
$my_array = array
( "ABC"
, "def" => 'GHI'
)
;
dump_r($my_array);

// AN INTERNAL PART OF AN ARRAY
dump_r($my_array[0]);
dump_r($my_array["def"]);

// AN OBJECT
class X
{
    public function __construct()
    {
        $this->x_property = "my X property";
    }
}
$x = new X;
dump_r($x);

// AN INTERNAL PART OF AN OBJECT
dump_r($x->x_property);

// A BOOLEAN
$T_or_F_bool = FALSE;
dump_r($T_or_F_bool);

// USING A RETURN STRING
$thing = 'My Thing';
$str = dump_r($thing, TRUE);
$arr = explode('=', $str);
foreach ($arr as $var)
{
    echo PHP_EOL . htmlentities(trim($var));
}
echo PHP_EOL;

//  NULL VARIABLE
$thing = NULL;
dump_r($thing);

// COMPARE VAR-DUMP OUTPUT
var_dump($thing);

Open in new window

Best to all, ~Ray
0
 
Dave BaldwinFixer of ProblemsCommented:
I can't find anything that will do that.  Even passing by reference is still a reference to the value, not the name.
0
 
Chris StanyonCommented:
My guess here is that you are misunderstanding programming principles, so rather than try and answer your question, I have to ask 'Why would you want to do that?' It makes no sense!

If you explain the 'why' we can probably tell you what you need to do, and it's unlikely to be echoing out the name of a variable
0
 
Dave BaldwinFixer of ProblemsCommented:
I did find one function that returns the names of variables, get_defined_vars.  Unfortunately, it returns all of them.  I didn't find a way to get only one.
http://www.php.net/manual/en/function.get-defined-vars.php
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.