Page 1 of 1

How to print variable name?

Posted: Sun Oct 19, 2003 6:47 pm
by scorphus
Hello dear mates,

Take a look to this C++ code I use for debug purposes:

Code: Select all

#include <iostream>
using namespace std;

#define print_var(var) cout << #var << ": " << var << endl

int main (void) &#123;
	int foo = 123;
	print_var(foo);
	return 0;
&#125;
and it's output:

Code: Select all

foo: 123
As you can see, #var prints the name of the variable. How would you do this in PHP?

Thanks,
Scorphus.

Posted: Sun Oct 19, 2003 6:49 pm
by volka

Code: Select all

<?php
function print_var($varname)
{
	global ${$varname};
	echo $varname, ': ', ${$varname};
}

$foo = (int)123;
print_var('foo'); 
?>
not exactly as your c code since you have to import the variable from global namespace (won't work within functions)