Solved

variable name needed to be converted to string

Posted on 2014-02-04
4
373 Views
Last Modified: 2014-02-24
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
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39834807
I can't find anything that will do that.  Even passing by reference is still a reference to the value, not the name.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39835165
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39835277
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39836288
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question