1

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?

6
  • 1
    Can't you just print_r everything you want dumped into the eMail message body, e.g. superglobals, debug_backtrace, etc? Commented Feb 10, 2010 at 12:53
  • What if there is more than one variable with the same value? Or if the variable is not in the global scope? Commented Feb 10, 2010 at 12:53
  • @Gordon: As far as I know print_r returns always 1, so I cannot. I could use var_dump tho. But it's not the point of question. I'm just wondering if it's possible to print name of variable. Sorry if I used misleading example. Commented Feb 10, 2010 at 12:55
  • 1
    @Ondrej: print_r($var, true) -- or for other functions which don't have an option to return instead of print, use Output Buffering. Commented Feb 10, 2010 at 12:59
  • Sorry, my fault, didn't realize it. However, it doesn't show me name of variable, and that's the whole point of question. get_defined_vars() seems to be a right answer. Commented Feb 10, 2010 at 13:04

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed. Appreciated :)
1

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

Comments

1

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.

Comments

1

how about get_defined_vars ? check out get_defined_functions and get_defined_constants as well

Comments

0

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}\"";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.