Solved

variable name needed to be converted to string

Posted on 2014-02-04
4
363 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 35
Popup to change image and store url in database 2 31
Site hacked - decoding the PHP? 15 66
PHP warning 4 30
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.

861 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now