how can i print var name instead var value in php? Like this code:
$Tree = "abcd";
$str = "zwd";
i want to print something like this: tree = abcd str = zwd
please help me
how can i print var name instead var value in php? Like this code:
$Tree = "abcd";
$str = "zwd";
i want to print something like this: tree = abcd str = zwd
please help me
You may want to take a look at compact function
<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", "nothing_here", $location_vars);
// print_r($result);
?>
Then print the key and its value,
foreach($result as $key => $value)
{
echo $key . " : " . $value;
}
What you maybe looking for is
echo 'Tree = ' . $Tree;
echo '<br>';
echo 'str = ' . $str;
I am pretty sure OP meant above, btw for expert level programmers, Why this question is paradoxical in nature, is lets say you want to print/output the name of variable $xyz
. See....
...did you notice...
....that in order to print the name of a variable you had to actually type the name of the variable in the first place! How else would you refer to that variable!!! And for that the above method of concatenation is best suited :)
Here someone with sane mentality tried to explain better.
I think what you want is this:
$array = array('Tree' => 'abcd', 'str' => 'zwd');
foreach ($array as $name => $value) {
echo $name . ' = ' . $value;
}
If you want to print variable names not writing it by yourself you should use get_defined_vars()
as follow :
$var1 = 'I am ';
$what = array_search($var1, get_defined_vars(), true);
printf($var1.'%s', $what); // I am var1