Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Output all PHP variables (See related posts)

// This outputs PHP variables for debugging. I didn't create it, here is the original source. Thanks to the creator for writing it: kailashbadu at hotmail dot com

// http://us.php.net/get_defined_vars


<?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 "
";
  print_r(getDefinedVars($varList, $excludeList));
  print "
"; ?>

You need to create an account or log in to post comments to this site.


Related Posts