Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created May 2, 2012 17:49
Show Gist options
  • Save xeoncross/2578651 to your computer and use it in GitHub Desktop.
Save xeoncross/2578651 to your computer and use it in GitHub Desktop.
Get the name of the variable (instead of it's value)
<?php
/*
* This is just a simple function that dumps the name of the variable(s)
* being inspected instead of the values as var_dump() does.
*/
function var_name()
{
$bt = debug_backtrace();
$src = file($bt[0]["file"]);
return preg_replace('~(.*var_name\(([^)]+)\).*)~', "$2", $src[$bt[0]['line'] - 1]);
}
$time = microtime(TRUE);
$var = 'hello';
$foo = $var;
// Simple
print var_name($var). "\n";
// More
print var_name($var, $foo). "\n";
// Trouble
print var_name($foo). "\n" . var_name($var) . "\n";
// Fine again
print var_name($foo). "\n"
. var_name($var) . "\n";
print (microtime(TRUE) - $time) . " ms\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment