Solved

variable name needed to be converted to string

Posted on 2014-02-04
4
355 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 82

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 42

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 82

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

16 Experts available now in Live!

Get 1:1 Help Now