1

I would like to be able to output the name of a variable, along with that variable's value. My use case is something close to a debug situation, but I'm actually building a proof of concept for other developers and managers so we can talk about things like input filtering. Anyway...

The output HTML is currently a table, but that shouldn't matter. I can of course, just print out the name and then the contents in the HTML, but that gets tedious and is prone to typing errors. I'd like a function that I could call with the variable name, or a string with the variable name as the argument, and have that function generate the appropriate HTML for display. This doesn't appear to work.

This works:

<tr><td>$variable</td><td><?php print $variable?></td></tr>

This doesn't:

function rowFromVar($varname) {
    $result  = "<tr>";
    $result .= "<td>\$$varname</td>";
    $result .= "<td>" . $$varname . "</td>";
    $result .= "</tr>";
    return $result;
}
// now in the HTML...
<table><?php print rowFromVar("variable");?></table>

The variable variable $$varname is always empty. Notice that I'm passing in the name of the variable as a string, rather than trying to pass in the variable itself and work with it. I know that there be dragons that way. Instead, I'm trying to pass in the name of the variable as a string, and then use a variable variable (yeah, I know) to return the value of the original variable. That appears not to work either, and I'm betting it's a scope issue.

Is there any good way to accomplish what I'm looking for?

Community
  • 1
  • 1
Don Faulkner
  • 121
  • 7

6 Answers6

1

The variable variable $$varname is always empty. Notice that I'm passing in the name of the variable as a string, rather than trying to pass in the variable itself and work with it

It should be empty

Since you are passing variable as value, so:

$$varname

becomes:

$variable

and there is no value set for it.

In other words, you are just creating a variable with the name you pass as argument to function but you are not creating a value for it.

Solution:

This would work fine though if that's what you are looking to do:

function rowFromVar($varname) {
    $result  = "<tr>";
    $result .= "<td>\$$varname</td>";
    $result .= "<td>" . $varname . "</td>";
    $result .= "</tr>";
    return $result;
}

// now in the HTML...
<table><?php print rowFromVar("variable");?></table>

Simply remove $ from the variable on this line as done above:

$result .= "<td>" . $$varname . "</td>";
--------------------^
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I don't think this does what I want. Say `$variable="myname"`. I want to get `$variablemyname`. When I take `$$varname` down to `$varname`, I get `$variablevariable`. In other words, I get the name of the variable again, not its contents. (Or perhaps I'm misunderstanding?) – Don Faulkner Jun 14 '12 at 15:45
  • This would simply print the name of the variable with a '$' before it and then the same name again without. What the question wants is to print the value of $variable assuming that it **is** set. – John Lawrence Jun 14 '12 at 15:47
  • 1
    @DonFaulkner: Did you even try it first? I had to create a demo due to your comment: See working example here: http://codepad.org/kzFbsFfS and not working example eg with your two `$$` here: http://codepad.org/mtUzVPi1 – Sarfraz Jun 14 '12 at 15:49
  • @Sarfraz your demo may work, but it does not do what the question asked! Your function takes a variable and then prints its value first preceeded by a '$' and then on its own. What the question asks for is to print a variable name and its value. Look at the other answers, see how they are different to what you are doing! – John Lawrence Jun 14 '12 at 16:19
  • 1
    @JohnLawrence: Variable name is also being printed if you could look at demo, `print rowFromVar("sarfraz");` outputs `$sarfrazsarfraz` – Sarfraz Jun 14 '12 at 16:56
  • Yes, but that is **not** what the question asks for! The problem here is that you do not understand what is being asked. Look at this example [http://codepad.org/AVSWMB23](http://codepad.org/AVSWMB23) This is doing exactly what was required! Don Faulkner the person who asked the question said that this was correct, yet still you don't see it. Please read the question more carefully. – John Lawrence Jun 14 '12 at 17:08
  • @JohnLawrence: That's creating confusion, let OP choose the correct answer or whichever helps him then I suppose so as to end this discussion. – Sarfraz Jun 14 '12 at 17:10
1

Did you try using $GLOBALS[$varname] instead of $$varname? Your variable in this example is at global scope, and you cannot directly access globals within a function.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
0

The following code should work, as you suspected scope is the main problem here.

function rowFromVar($varname, $contents) {
    $result  = "<tr>";
    $result .= "<td>\$$varname</td>";
    $result .= "<td>" . $contents . "</td>";
    $result .= "</tr>";
    return $result;
}
// now in the HTML...
<table><?php print rowFromVar("variable", $variable);?></table>
Dan Smith
  • 5,685
  • 32
  • 33
  • Yes, that will work, but it only partially solves my problem. I still have to type "variable" twice, and that's what I was chiefly trying to avoid. – Don Faulkner Jun 14 '12 at 15:41
0

You need declare your variable global within the function so that it references the global version. Adding this line at the start of your function should fix the problem:

function rowFromVar($varname) {
    global $$varname;
    $result  = "<tr>";
    $result .= "<td>\$$varname</td>";
    $result .= "<td>" . $$varname . "</td>";
    $result .= "</tr>";
    return $result;
}
John Lawrence
  • 2,923
  • 17
  • 23
0

Please try this code:

function rowFromVar($varname) {
    $result  = "<tr>";
    $result .= "<td>{$varname}</td>";
    $result .= "<td>".$varname."</td>";
    $result .= "</tr>";
    return $result;
}

<table><?php echo rowFromVar("variable");?></table>
Bunlong
  • 652
  • 1
  • 9
  • 21
0

I had a similar problem, which I solved as follows.

function contents_of($string)
{
  eval("global $string; \$value = $string;");
  return "Variable $string has value $value";
}

$testing = 123;
echo contents_of('$testing');

// produces...
// Variable $testing has value 123
D.Gibson
  • 79
  • 6