Created
May 2, 2012 17:49
-
-
Save xeoncross/2578651 to your computer and use it in GitHub Desktop.
Get the name of the variable (instead of it's value)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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