note 73608 added to function.get-defined-vars

From: Date: Sat, 03 Mar 2007 11:09:45 +0000
Subject: note 73608 added to function.get-defined-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-124039@lists.php.net to get a copy of this message
After a fruitless attempt find a built-in function whic did this, I wrote this functions to find out all the variables (well, those I wanted) in current scope and their values. I believe this is going to be handy in debugging. <?php /** * @desc works out the variables in the current scope(from where function was called). * Returns an array with variable name as key and vaiable value as value * @param $varList: variables returned by get_defined_vars() in desired scope. * $excludeList: variables to be excluded from the list. * @return array */ function getDefinedVars($varList, $excludeList) { $temp1 = array_values(array_diff(array_keys($varList), $excludeList)); $temp2 = array(); while (list($key, $value) = each($temp1)) { global $$value; $temp2[$value] = $$value; } return $temp2; } /** * @desc holds the variable that are to be excluded from the list. * Add or drop new elements as per your preference. * @var array */ $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList'); //some dummy variables; add your own or include a file. $firstName = 'kailash'; $lastName = 'Badu'; $test = array('Pratistha', 'sanu', 'fuchhi'); //get all variables defined in current scope $varList = get_defined_vars(); //Time to call the function print "<pre>"; print_r(getDefinedVars($varList, $excludeList)); print "</pre>"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 202.161.131.71 (proxied: 202.166.211.212) ---- Manual Page -- http://www.php.net/manual/en/function.get-defined-vars.php Edit -- https://master.php.net/note/edit/73608 Del: integrated -- https://master.php.net/note/delete/73608/integrated Del: useless -- https://master.php.net/note/delete/73608/useless Del: bad code -- https://master.php.net/note/delete/73608/bad+code Del: spam -- https://master.php.net/note/delete/73608/spam Del: non-english -- https://master.php.net/note/delete/73608/non-english Del: in docs -- https://master.php.net/note/delete/73608/in+docs Del: other reasons-- https://master.php.net/note/delete/73608 Reject -- https://master.php.net/note/reject/73608 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124039) next »