php dump 当前所有局部变量
虽然有xdebug,平时还是用log或是var_dump调试的时候居多。
经常想知道执行某个点时当前的变量情况,今天回来找了下参考php官网上 kailashbadu 的代码写了一个。
核心在于 get_defined_vars() 函数。另外也有取出所有常量的get_defined_constants() 函数。
/** * @desc show the variable that are to be excluded from the list. * $varList array * $excludeList array */ function logVars($varList, $excludeList = null) { if ($excludeList == null) $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET'); $list = array(); foreach ($varList as $key => $value) { if (!in_array($key, $excludeList)) $list[$key] = $value; } echo '<pre>'; print_r($list); echo '</pre>'; } //some dummy variables; add your own or include a file. $firstName = 'kailash'; $lastName = 'Badu'; $test = array('Pratistha', 'sanu', 'fuchhi'); logVars(get_defined_vars()); // results /* Array ( [firstName] => kailash [lastName] => Badu [test] => Array ( [0] => Pratistha [1] => sanu [2] => fuchhi ) ) */
参考: http://cn.php.net/manual/en/function.get-defined-vars.php