Hi
How can I achieve the same effect in PHP as with Python's str-function? That is, convert a variable (e.g. $var) to its equivalent string representation ("var").
Regards tores
convert variables to strings
Moderator: General Moderators
-
vietnamese
- Forum Newbie
- Posts: 7
- Joined: Tue Dec 21, 2004 2:33 am
-
vietnamese
- Forum Newbie
- Posts: 7
- Joined: Tue Dec 21, 2004 2:33 am
okey... This is exactly what I want...
I have this small function:
This function just prints "DEBUG: <value of $var>"... This way I have to look at the value printed to figure out which variable is dumped (if I use this function in several places)... It would be a lot easier if I could print something like "DEBUG: <name of $var> <value of $var>". <name of $var> should then refer to the name of the parameter used when calling dump... Maybe this is impossible without specifying another parameter like
dump($var_value, $var_name)? If not, I would like to know...
regards tores
I have this small function:
Code: Select all
<?php
function dump($var){
echo "DEBUG: ";
var_dump($var);
echo "<br>";
}
?>dump($var_value, $var_name)? If not, I would like to know...
regards tores
i understand exactly what you mean.
unfortunately i think you will need to modify the function like you suggested
dump('$foo', $foo);
this limitations comes from putting this into a function. the variable loses its "name" inside the scope of a function, only the value is passed.
but this should work for variables in the global scope, but wont really work w/ nested variables
maybe you could play around w/ eval() to make it work w/ a greater range of variables, or, just pass 2 parameters to your origional function
unfortunately i think you will need to modify the function like you suggested
dump('$foo', $foo);
this limitations comes from putting this into a function. the variable loses its "name" inside the scope of a function, only the value is passed.
but this should work for variables in the global scope, but wont really work w/ nested variables
maybe you could play around w/ eval() to make it work w/ a greater range of variables, or, just pass 2 parameters to your origional function
Code: Select all
$foo = 'bar';
function dump($var) {
echo '$' . $var . ' => ';
var_dump($GLOBALS[$var]);
}
dump('foo');