convert variables to strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

convert variables to strings

Post by tores »

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
vietnamese
Forum Newbie
Posts: 7
Joined: Tue Dec 21, 2004 2:33 am

Post by vietnamese »

use (string) if variable is number type, datetime...
sample
$temp = 123456;
$temp = (string) $temp;
however variable is array, you can use function serialize($array) to convert a array to string, and convert that string to array, use function unserialize($string)
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

Sorry for not beeing spesific. I don't want to convert the variable's contents to a string. It's the variable-name itself I want to convert. The reason for this is that when dumping a variable I also want the variable's name printed out...

Regards tores
vietnamese
Forum Newbie
Posts: 7
Joined: Tue Dec 21, 2004 2:33 am

Post by vietnamese »

he he !! if you want print name of $value, you use echo "value"; when you use a variable, you must know variable's name so that, you can create a avariable contant that name string.
I don't understand why you need dumping a avariable as a string??
can you show me??
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

okey... This is exactly what I want...

I have this small function:

Code: Select all

<?php
function dump($var){
  echo "DEBUG: ";
  var_dump($var);
  echo "<br>";  
}

?>
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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

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

Code: Select all

$foo = 'bar';

function dump($var) {
    echo '$' . $var . ' => ';
    var_dump($GLOBALS[$var]);
}

dump('foo');
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

or play around with variable variables

[php_man]variables.variable[/php_man]
Post Reply