Here is a function which generates a debug report for display or emailusing get_defined_vars. Great for getting a detailed snapshot withoutrelying on user input.

<?phpfunction generateDebugReport($method,$defined_vars,$email="undefined"){// Function to create a debug report to display or email. // Usage: generateDebugReport(method,get_defined_vars(),email[optional]); // Where method is "browser" or "email".

// Create an ignore list for keys returned by 'get_defined_vars'. // For example, HTTP_POST_VARS, HTTP_GET_VARS and others are // redundant (same as _POST, _GET) // Also include vars you want ignored for security reasons - i.e. PHPSESSID.$ignorelist=array("HTTP_POST_VARS","HTTP_GET_VARS","HTTP_COOKIE_VARS","HTTP_SERVER_VARS","HTTP_ENV_VARS","HTTP_SESSION_VARS","_ENV","PHPSESSID","SESS_DBUSER","SESS_DBPASS","HTTP_COOKIE");

// Get the last SQL error for good measure, where $link is the resource identifier // for mysql_connect. Comment out or modify for your database or abstraction setup.global $link;$sql_error=mysql_error($link); if($sql_error){$message.="\nMysql Messages:\n".mysql_error($link); }// End MySQL

// now try to change the value through the returned array$defined_vars["my_var"] = "bar";

echo $my_var, "\n";

?>

will output "foo" (the original value). It'd be nice if get_defined_vars() had an optional argument to make them references, but I imagine its a rather specialized request. You can do it yourself (less conveniently) with something like:

Here's a very simple function for debugging. It's far from perfect but I find it very handy. It outputs the var value and the var name on a new line. The problem is it'll echo any vars and their name if they share the same value. No big deal when debugging and saves the hassle of writing the HTML and var name when echoing a variable. (ev=echo variable). Using get_defined_vars() inside a function renames the var name to the functions variable so isn't as useful for debugging. Of course, you'll need access to the $GLOBALS array<?function ev($variable){ foreach($GLOBALS as $key => $value){ if($variable===$value){ echo '<p>'.$key.' - '.$value.'</p>'; } }}

Note that originally I had an array of the super-globals I wanted removed from get_defined_vars()'s array, then I noticed even an empty double-array, array(array()), made it give me the correct result. Weird.

Please be aware that function_get_vars only returns the variables defined at the point before you call the function_get_vars function, it does not scan the entire function for you, only the lines before you call it.