tags:

views:

80

answers:

5

I'm writing a simple script for my webby that would send me an e-mail in case error 500 happens. I want to dump all possible variables, sessions, POSTs or whatever, present at time when error happened, so I can analyze the problem as precise as I can.

Here's the code I have for now:

function variable_name( &$var ) {
    $var_name = array_search( $var, $GLOBALS );
    return "{$var_name} = \"{$var}\"";
}

$bar = "whatever";

echo variable_name( $bar ); // bar = "whatever"

It's checking just $GLOBALS, but I need something that would check and print also $_POST, $_SESSION, class fields etc. I googled a bit and found just complicated functions that seem like an overkill for such easy task. Is there anything simple for this purpose or should I simply write a function for each of variable types?

+4  A: 

use get_defined_vars():

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

this includes $GLOBALS (since PHP 5.0.0), $_POST, $_SESSION, etc.

ax
Exactly what I needed. Appreciated :)
Ondrej Slinták
+1  A: 

Have you tried the get_defined_vars function ? http://fr2.php.net/manual/fr/function.get-defined-vars.php

Zed-K
+1  A: 

Ondrej, try the print_r function. Look it up in the manual, it's really helpful at times. Notice it also takes an optional second parameter which is useful when you want to log the output to a file or db instead of just printing it to screen.

Peter Perháč
print_r doesn't show me actual name of variable.
Ondrej Slinták
+1  A: 

how about get_defined_vars ? check out get_defined_functions and get_defined_constants as well

ghostdog74
A: 

but I need something that would check and print also $_POST, $_SESSION, class fields etc.

How about this:

function variable_name(&$var) {
    $var_name = array_search( $var, $_POST); // or you can put $_GET, $_SEESION,etc 
    return "{$var_name} = \"{$var}\"";
}
Sarfraz