Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.28 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.   /**
  3.    * @desc   works out the variables in the current scope(from where function was called).
  4.    *         Returns an array with variable name as key and vaiable value as value
  5.    * @param  $varList: variables returned by get_defined_vars() in desired scope.
  6.    *         $excludeList: variables to be excluded from the list.
  7.    * @return array
  8.    */
  9.   function getDefinedVars($varList, $excludeList)
  10.   {
  11.       $temp1 = array_values(array_diff(array_keys($varList), $excludeList));
  12.       $temp2 = array();
  13.       while (list($key, $value) = each($temp1)) {
  14.           global $$value;
  15.           $temp2[$value] = $$value;
  16.       }
  17.       return $temp2;
  18.   }
  19.  
  20.   /**
  21.    * @desc   holds the variable that are to be excluded from the list.
  22.    *         Add or drop new elements as per your preference.
  23.    * @var    array
  24.    */
  25.   $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList');
  26.  
  27.   //some dummy variables; add your own or include a file.
  28.   $firstName = 'kailash';
  29.   $lastName = 'Badu';
  30.   $test = array('Pratistha', 'sanu', 'fuchhi');
  31.  
  32.   //get all variables defined in current scope
  33.   $varList = get_defined_vars();
  34.  
  35.   //Time to call the function
  36.   print "<pre>";
  37.   print_r(getDefinedVars($varList, $excludeList));
  38.   print "</pre>";
  39. ?>