?
Solved

variable name needed to be converted to string

Posted on 2014-02-04
4
Medium Priority
?
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

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 44

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 84

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

718 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